Shabbir Panjesha Shabbir Panjesha - 1 year ago 180
Git Question

Execute commands from .bat file on Git Bash Terminal

I am newbie to Git bash.

Just out of curiosity trying to make a .bat file which contains commands(Dont know if Git Bash supports .bat file)

What I want to achieve is simply drag and drop this .bat file to Git Bash terminal
and commands in the file get executed(Is it possible?).

My commands in .bat file

cd "C:\Users\USER\abc\xyz"
cd "C:\Users\USER\abc\xyz\pqr"
export HOME="C:\Users\USER\some_directory"
export HOME2="C:\Program Files\directoy"

Answer Source

Git Bash can execute bash scripts, but not bat scripts. If you rename the file to and rewrite like this, then Git Bash should be able to execute it:

cd 'C:\Users\USER\abc\xyz'
cd 'C:\Users\USER\abc\xyz\pqr'
export HOME='C:\Users\USER\some_directory'
export HOME2='C:\Program Files\directoy'

I replaced the double quotes with single quotes. Otherwise the backslashes would be used as escape characters, for example Git Bash would try to execute cd C:UsersUSERabcxyz. Actually it's better to write paths in Git Bash like this:

cd '/c/Users/USER/abc/xyz'
cd '/c/Users/USER/abc/xyz/pqr'
export HOME='/c/Users/USER/some_directory'
export HOME2='/c/Program Files/directoy'

In any case, I think what you're looking for is NOT "executing", but "sourcing". If you source this script, the commands will be executed in your current shell. So HOME and HOME2 will be set in your current shell. If you execute this, that will launch a new shell, which will execute the cd commands and set HOME and HOME2 but those settings won't affect your current shell.

It's possible to create files and execute them by double clicking. But it is not possible to source them by double clicking, or to have them sourced by drag and drop to a Git Bash window.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download