3

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?

ThePiachu
  • 8,695
  • 17
  • 65
  • 94
  • The file association is probably incomplete. You can view and edit file associations with the `ftype` and `assoc` commands. `ftype /?` for help. – Harry Johnston Aug 20 '15 at 01:06
  • @HarryJohnston from assoc I see `.sh=sh_auto_file` and from ftype - `sh_auto_file="C:\Program Files\Git\git-bash.exe" --no-cd "%L" %*` . What should it be instead and how do I change it? – ThePiachu Aug 20 '15 at 03:22
  • Try adding `#!/usr/bin/env bash` as the first line of the `sh` file. – Elliott Frisch Aug 20 '15 at 03:59
  • 1
    Well, the association already passes the arguments along, so that looks OK. It is running `git-bash.exe` rather than `sh` but I guess those are synonyms? What does `git-bash test.sh 123` do? – Harry Johnston Aug 20 '15 at 04:54
  • It prints `Test 123`. My other computer's configuration is `sh_auto_file="C:\Program Files (x86)\Git\bin\sh.exe" "--login" "%1" %*` . By the way, how do I change the ftype configuration? – ThePiachu Aug 20 '15 at 17:36
  • 1
    Using the `ftype` command as per the help, e.g., `ftype sh_auto_file=git-bash "%1" %*` – Harry Johnston Aug 20 '15 at 21:56
  • @HarryJohnston Using the ftype command apparently didn't solve the issue. Turns out there were 2 or 3 places in registry that had to be switched manually - a lot of them either didn't have the %1 %* at the end and some were referencing some wrong .exe file... At least everything is working. – ThePiachu Aug 26 '15 at 01:50

1 Answers1

0

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.

ThePiachu
  • 8,695
  • 17
  • 65
  • 94