10

Does bash and sh execute a script differently? I executed a shell script with both bash and sh, and got the same output. So what is the difference?

Also, when you just run ./executable, does it use bash or sh?

4
  • 1
    @Pandya The $SHELL variable is not used to decide what shell will interpret the ./executable script. It doesn't necessarily tell what the current shell is either.
    – jlliagre
    Commented Mar 20, 2016 at 8:19
  • @jlliagre oh! ok. wiki.debian.org says "SHELL = The current shell."
    – Pandya
    Commented Mar 20, 2016 at 10:54
  • @Pandya it actually should say "The user's login shell". The current shell might be different.
    – jlliagre
    Commented Mar 20, 2016 at 11:25
  • For another example, you can consider my answer here, where #!/bin/sh instead of #!/bin/bash led to the usage of a wrong version of printf causing printf: %q: invalid directive. M.T pointed me to this Q/A, thanks for that.
    – Cadoiz
    Commented Oct 19, 2021 at 15:31

2 Answers 2

14

That depends on the system you are running. One many OSes, especially Linux based ones, sh is a link to bash.

In such case, there are still some differences in behavior where bash try to be more like traditional bourne shell when called sh, but it still accepts most bashisms.

On some other OSes, like Debian based ones, sh is provided by dash, not bash. That makes a much bigger difference as dash doesn't support bashisms, being designed to be a clean POSIX shell implementation.

On proprietary OSes, sh is often either provided by a POSIX compliant ksh88 which like dash, doesn't implement bashisms. On Solaris 10 and older, depending on what your PATH is, sh will likely be a legacy Bourne shell, predating POSIX.

In any case, you likely got the same output with your test simply because your script was not using any bash specific command, option or syntax.

When you run ./executable, what shell will be run essentially depends on the shebang written at the beginning of the .executable script. That will be bash if the shebang specifies it:

#!/bin/bash
....

If there is no shebang and you call the script from a POSIX compliant shell, the script should technically be executed by the first sh found in the PATH. Many shell interpreters like bash, dash and ksh are considering themselves to be POSIX so will interpret the script. Note that the SHELL environment variable is not used here.

4
  • I looked around in /bin and I found that sh is a symbolic link to dash rather than bash. So how did they run the same? It wasn't really running any bash script commands, just terminal commands to use other packages' tools.
    – Rohan
    Commented Mar 20, 2016 at 6:09
  • 1
    If your shell script is just sequentially calling other commands, any shell interpreter will run it with the same result. Nothing unexpected here.
    – jlliagre
    Commented Mar 20, 2016 at 8:21
  • This isn't related to the question, but what exactly is the difference between bash and dash?
    – Rohan
    Commented Mar 20, 2016 at 19:20
  • Both are using the Bourne shell syntax. As I wrote in my reply, dash is a clean implementation of the POSIX shell standard. On the other hand, bash is a shell with quite a large number of extensions and non standard features, some inspired by ksh, ksh93 and other shells, and some bash specific, known as bashisms. There is a tool named checkbashisms that helps identifying everything that is not strictly portable in a given shell script.
    – jlliagre
    Commented Mar 20, 2016 at 20:07
5

Yes it could be, but it does not have to be. The two shells are not the same, but the basics are compatible. Check this post for more details.

You can use the following line in a script to check the interpreter that is used:

ps h -p $$ -o args='' | cut -f1 -d' '

You can explicitly specify the shell that shall be used when the script is executed directly with the sheebang line:

#!/bin/sh

or

#!/bin/bash

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .