Execute commands from .bat file on Git Bash Terminal

You can run batch files just from git bash e.g.

 ./clear.bat 

From Out of a git console: how do I execute a batch file and then return to git console?


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?).

This is not possible, and probably will never be, because it's not a natural UX. Dragging a file from a file explorer to a Git Bash terminal should produce the absolute path to the file in the terminal. Then you can press enter to execute it. The natural way to execute a file in a file explorer is to double-click on it. (The file explorer may need configuration to allow the execution of .bat and .sh files on double-click.)

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"

For one thing, this script looks artificial: cd /some/abs/path followed by cd /some/other/abs/path is a pointless statement.

For another thing, the .bat extension should be used for DOS shell scripts, but the export command doesn't exist in DOS (it exists in Bash). So your example should be a .sh script, not a .bat script.

Lastly, it's important to understand the distinction between executing a script and sourcing a script:

  • When you execute a script, for example with path/to/script.sh, the commands in it are executed in a child process. As such, commands that change the execution environment, such as changing the directory or variables, will apply only to the child process. In other words, the effect of cd and export commands will not be visible when the script exits.

  • When you source a script, for example with source path/to/script.sh (or . path/to/script.sh), the commands in it are executed in the current process. As such, commands that change the execution environment, such as changing the directory or variables, will apply to the current process.

In other words, if you want cd and export commands to have an effect in the current shell, then you want to source the script, instead of executing.

Tags:

Git

Git Bash