在Git Bash终端上执行.bat文件中的命令 [英] Execute commands from .bat file on Git Bash Terminal
问题描述
出于好奇,试图制作一个包含命令的.bat文件(不知道Git Bash是否支持.bat文件)
我想要实现的只是将这个.bat文件拖放到Git Bash终端
并且文件中的命令被执行(这可能吗?) 。
.bat文件中的命令
cdC: \ Users \USER\abc\xyz
cdC:\Users\USER\abc\xyz\pqr
export HOME =C:\Users \USER\some_directory
export HOME2 =C:\ Program Files \directoy
Git Bash可以执行bash脚本,但不能执行bat脚本。如果您将文件重命名为 script.sh
并重写为这样,那么Git Bash应该能够执行它:
#!/ bin / bash
cd'C:\Users\USER\abc\xyz'
cd'C:\Users\\ \\USER\abc\xyz\pqr'
export HOME ='C:\Users\USER\some_directory'
export HOME2 ='C:\程序文件\directoy'
我用单引号替换了双引号。否则,反斜杠将用作转义字符,例如Git Bash会尝试执行 cd C:UsersUSERabcxyz
。其实最好是在Git Bash中写下这样的路径:
#!/ bin / bash
cd'/ c / Users / USER / abc / xyz'
cd'/ c / Users / USER / abc / xyz / pqr'
export HOME ='/ c / Users / USER / some_directory'
导出HOME2 ='/ c / Program Files / directoy'
无论如何,我想你是什么寻找不是执行,而是采购。如果你输入这个脚本,这些命令将在当前shell中执行。因此, HOME
和 HOME2
将在您当前的shell中设置。如果执行此操作,它将启动一个新的shell,它将执行 cd
命令并设置 HOME
和 HOME2
,但这些设置不会影响当前的shell。
可以创建脚本。 sh
文件并通过双击执行它们。但是不能通过双击来源,或者通过拖放到Git Bash窗口来获取它们。
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"
Git Bash can execute bash scripts, but not bat scripts. If you rename the file to script.sh
and rewrite like this, then Git Bash should be able to execute it:
#!/bin/bash
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:
#!/bin/bash
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 script.sh
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.
这篇关于在Git Bash终端上执行.bat文件中的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!