18

I have bash script and it requires bash.

Another person try to run it with

sh script_name.sh

And it fails because sh is symbolic link to dash in his distribution.

$ ls -la /bin/sh
lrwxrwxrwx 1 root root 4 Aug 25 16:06 /bin/sh -> dash

I have an idea to use wrapper script:

#!/bin/sh
bash script_name.sh

The goal is to run .sh script by sh with bash in system having symbolic link to dash.

  • What if you just do `./script_name.sh`? – fedorqui Oct 23 '13 at 10:07
  • 2
    Or get the other user to run with `bash script_name.sh`? - A wrapper will work too. What is your question? – John3136 Oct 23 '13 at 10:07
  • You should show your script. What you want is to change your script so that it conforms strictly to Posix `sh` standard. – Basile Starynkevitch Oct 23 '13 at 10:17
  • strange, if `/bin/sh` points to dash so maybe there is no real `/bin/sh` on this system – mzet Oct 23 '13 at 10:20
  • It is really huge and has strong dependency on bash (Arrays, functions, etc.). –  Oct 23 '13 at 10:21
  • make sure (on that others person system) that bash is installed – mzet Oct 23 '13 at 10:23
  • 2
    Then you should state that `bash` is explicitly required, that the script should be run with `/bin/bash script_name.sh`, and start that script with `#!/bin/bash` .... – Basile Starynkevitch Oct 23 '13 at 10:23
  • Yes, it is intalled. For example clean Debian installation has symbolic link /bin/sh -> dash. But of course, it has bash. –  Oct 23 '13 at 10:25
  • @mzet Most systems do not have a separate `/bin/sh` these days. `/bin/sh` is a "virtual" shell, which can be implemented by any shell that supports the POSIX standard. – chepner Oct 23 '13 at 13:16
  • Rename your script `script_name.bash` to emphasize that it is not POSIX-compliant. It's not your problem if someone tries to run a script with the wrong interpreter. – chepner Oct 23 '13 at 13:17
  • @mzet, what do you mean "no real `/bin/sh`"? dash _is_ a real `/bin/sh`; the only thing `/bin/sh` is required to be is a POSIX-compliant shell; it's allowed to be _any_ POSIX-compliant shell, and dash is one such shell. POSIX.2 has been the controlling standard since the early 90s; it's been many, many decades since there was One True Bourne Shell. – Charles Duffy Aug 24 '22 at 02:06

2 Answers2

35

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
neuro
  • 14,948
  • 3
  • 36
  • 59
4

In your script before you anything else, you can do something like:

if [ "$BASH" != "/bin/bash" ]; then
  echo "Please do ./$0"
  exit 1
fi

or the more general way is using $BASH_VERSION:

if [ -z "$BASH_VERSION" ]; then
  echo "Please do ./$0"
  exit 1
fi
jbr
  • 6,198
  • 3
  • 30
  • 42