Run bash script with sh
Well, usually you use the shebang to tell the shell to use the correct interpreter:
#!/bin/bash
# your script here
You have to set the script to be executable:
chmod +x my_script.sh
And let the user start it with:
./my_script.sh
It seems simple than to use a wrapper script.
You can use jbr test to run your script with bash even if the user use sh/dash or any sh like interpreter:
#!/bin/bash
if [ -z "$BASH_VERSION" ]
then
exec bash "$0" "$@"
fi
# Your script here
This way it correctly works with either :
sh ./my_script.sh
# or
bash ./my_script.sh
# or
./my_script.sh
Running .sh scripts in Git Bash
Let's say you have a script script.sh
. To run it (using Git Bash), you do the following: [a] Add a "sh-bang" line on the first line (e.g. #!/bin/bash
) and then [b]:
# Use ./ (or any valid dir spec):
./script.sh
Note: chmod +x
does nothing to a script's executability on Git Bash. It won't hurt to run it, but it won't accomplish anything either.
How to run .sh on Windows Command Prompt?
The error message indicates that you have not installed bash
, or it is not in your PATH
.
The top Google hit is http://win-bash.sourceforge.net/ but you also need to understand that most Bash scripts expect a Unix-like environment; so just installing Bash is probably unlikely to allow you to run a script you found on the net, unless it was specifically designed for this particular usage scenario. The usual solution to that is https://www.cygwin.com/ but there are many possible alternatives, depending on what exactly it is that you want to accomplish.
If Windows is not central to your usage scenario, installing a free OS (perhaps virtualized) might be the simplest way forward.
The second error message is due to the fact that Windows nominally accepts forward slash as a directory separator, but in this context, it is being interpreted as a switch separator. In other words, Windows parses your command line as app /build /build.sh
(or, to paraphrase with Unix option conventions, app --build --build.sh
). You could try app\build\build.sh
but it is unlikely to work, because of the circumstances outlined above.
Do bash scripts execute in new shells or subshells?
You're correct; when you run a script with ./shell.sh
, it runs in a new shell, not a subshell of the current shell.
It does run in a subprocess, which is a shell, so it's a tempting and common mistake to say "subprocess+shell=subshell, so it must be a subshell!" But that's incorrect. The shell running the script won't inherit shell variables from the parent shell process (it'll inherit environment variables, i.e. export
ed variables, but that's true of any subprocess), it won't inherit shell modes (e.g. set -e
) or other shell state, and it won't even necessarily be running the same shell (if you're running bash and the script has a #!/bin/zsh
shebang, it'll run in zsh). So it's logically a different shell that just happens to be running as a subprocess of the shell that launched it.
How to run a .sh script from R on Windows?
seq
is part of coreutils so your first check should be if your Cygwin install has coreutils installed. You can find out how to install new packages on Cygwin here.
bash
under Cygwin will inherit the path from Windows and this doesn't include /usr/bin
. To fix this, tell it to behave as if it is invoked at login
all_arguments <- c("-l", scriptPath, parameters)
Note that the filenames will need to be recognisable to Cygwin. See https://cygwin.com/cygwin-ug-net/using.html#cygdrive
Shell script - Bad substitution Error while running script with sh
To make my comment an answer:
You're running with sh
, but your script declares it's a bash
script. On many systems sh
is not bash
, but a lighter shell that doesn't support all bashisms.
Either
- run with
bash test.sh
, or - mark the file
chmod u+x
and run./test.sh
to use the shebang line.
Related Topics
Compress Files While Reading Data from Stdin
How to Check the Bios Version or Name in Linux Through a Command Prompt
Linux Script with Curl to Check Webservice Is Up
Linux: How to Know the Module That Exports a Device Node
How to Load Luks Passphrase from Usb, Falling Back to Keyboard
How to Hide Wget Output in Linux
How to Find Lines Containing a String in Linux
Best Distributed Filesystem for Commodity Linux Storage Farm
Shell Init Issue When Click Tab, What's Wrong with Getcwd
Sanitize Environment with Command or Bash Script
Does Linux Kill Background Processes If We Close the Terminal from Which It Has Started
How I Should Run My Golang Process in Background
Linux Nasm Assembly Print All Numbers from Zero to 100
Groovy Process Not Working with Linux Shell (Grep and Awk and Ps)
How to Force a Cifs Connection to Unmount
For Loop in Bash Simply Prints N Times the Command Instead of Reiterating