Running .bat file on cygwin CLI without the .bat extension

3

I have been looking for a way to run dos batch files from the cygwin command line without having the enter the extension.

Would that be possible?

Currently, I have to enter the extension otherwise cygwin does not find the batch file.

Richard Lalancette

Posted 2012-12-20T01:50:39.330

Reputation: 133

Why do you want that? Anyway if your batch file is file.bat, try: 'cmd /C file' – golimar – 2013-01-11T10:18:48.223

1As a work around you can add alias command='command.bat' in your shell .profile – kode – 2014-02-08T22:25:52.853

Answers

2

I found no other solution so I did what golimar said.
Here is a simple script that looks for .bat files in a specific directory and creates aliases.
For example if there is a file git.bat in /some/path there will be a alias git that points to it.
Add this to your ~/.bashrc or ~/.zshrc or whatever you're using:

for f in /some/path/*.bat; do alias `basename "${f%.bat}"`=$f; done

fnkr

Posted 2012-12-20T01:50:39.330

Reputation: 530

2

another workaround is to use the bash internal function command_not_found_handle() like this:

command_not_found_handle() {
    echo "bash: $1: command_not_found_handle()"  >&2
    LOWERCASE_CMD=$(echo "$1" | tr '[A-Z]' '[a-z]')
    shift
    [ -f /cygdrive/c/CLIPrograms/${LOWERCASE_CMD}.bat ] && /cygdrive/c/CLIPrograms/${LOWERCASE_CMD}.bat "$@"
    return $?

eadmaster

Posted 2012-12-20T01:50:39.330

Reputation: 706

This is pretty awesome! Several pointers about your bash code. If using a variable inside a function, make sure you define it as local (unless you have a reason to create a global var. In my opinion, it is good style to use lower case for variables and upper case for (pseudo-)constants, although tastes vary. You can use ${1,,} to lowercase $1, no need for a temp var, subshell and pipe. In case the .bat file doesn't exist, you want to print the standard "bash: $1: command not found" and return 127. – Gene Pavlovsky – 2016-11-16T00:11:23.517