Execute commands from .bat file on Git Bash Terminal

早过忘川 提交于 2019-12-09 08:13:58

问题


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"

回答1:


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.




回答2:


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?



来源:https://stackoverflow.com/questions/22397521/execute-commands-from-bat-file-on-git-bash-terminal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!