How to pass parameters to an .sh script under Wind

2019-08-11 02:13发布

问题:

I'm trying to execute .sh scripts under Windows. I installed Git, which allowed me to execute .sh files. However, I can't seem to pass any parameters around without prefixing the execution with "sh":

My .sh file:

echo "Test" $1

if I execute it with:

> sh test.sh 123

I get

Test 123

However, if I just execute it as

> test.sh 123

I get

Test

I know it is possible to execute .sh scripts with parameters and without prefixing them with "sh", since through some combination of configurations I was able to do it on my old computer somehow. Any guesses as to how to accomplish this?

回答1:

As explained by @HarryJohnston in the comments, the first step to solving this problem was figuring out the assoc and ftype associations. assoc will most likely return .sh=sh_auto_file, while in my case, ftype returned sh_auto_file="C:\Program Files\Git\git-bash.exe" --no-cd "%L" %*. One part of the issue was similar to what was happening in this issue - Windows was not passing parameters to the .sh file. To fix that, the sh_auto_file association needs to end with / have "%1" %* as a part of it apparently. However, another part of the problem was that (at least on Windows 10 in my case), there might be multiple places in the registry that have a wrong record. In the end I had to manually change 2-3 entries in registry to "C:\Program Files (x86)\Git\bin\sh.exe" "%1" %* in order for it to finally work. To find those places, search for .sh and sh_auto_file and replace accordingly.

Bonus: To make .sh files execute without having to write ".sh" at the end, add .SH to pathext in environmental variables.