4

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.

2
  • Why do you want that? Anyway if your batch file is file.bat, try: 'cmd /C file'
    – golimar
    Jan 11, 2013 at 10:18
  • 1
    As a work around you can add alias command='command.bat' in your shell .profile
    – kode
    Feb 8, 2014 at 22:25

2 Answers 2

3

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
3

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 $?
1
  • 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. Nov 16, 2016 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.