What is the purpose of the `sh` command?

What is the purpose of the sh command when used interactively and when used inside of a bash script?

Other than on the hash bang line (the first line) should sh ever be replaced with bash on a bash system?

Example:

#!/bin/bash
sh -c "command"

So many questions... I'll take them one at a time.

1) What is the purpose of the sh command when used interactively? Provides a new environment context, so if you wanted to experiment with some environment variable setting, you could. And when you were done, exit out and no harm done.

Also, if you were in a different shell like zsh or csh, and wanted to execute in a sh shell, this would switch you.

2) When used in a bash script like you have above, it will again provide a contained environment context for "command" to run in. You can also use it with the

#!/bin/bash
sh -c "command" &
and fork off "command" to run in parallel with the rest of your script.

3) I would say, if it is something that specifically needs bash, then explicitly put it in your hash bang line. But since on most later *nix systems sh is equivalent to bash, it is probably not necessary.


What happens when I execute sh?

If you simply start sh on the shell, you'll start another shell inside your current shell.

What happens when I use sh in a script?

The same thing, you start another shell inside the shell that is processing the script.

Should I simply use /bin/bash if that's what /bin/sh points to anyway?

No. If the script was designed to work with any shell, then it is designed to work against the lowest common denominator between all shells (to my understanding, this is part of the POSIX standard).
Changing to a specific interpreter shouldn't be harmful (because, all of them comply to the same standard) but it reduces compatibility while providing no benefit at all.

So, I should always use /bin/sh then?

As an interpreter

No. If you're writing a script yourself, then you can also use a more specific interpreter (like bash) if that interpreter provides additional features you want to make use of. But keep in mind that everyone who uses your script will be required to have your chosen interpreter.

To start another process

If you simply want to start a new process in its own context, then, by all means, use /bin/sh.

So what's the point of it all?

If you want to execute something in its own context, you should execute it in a new shell.
The easiest way to achieve that is to start a new shell (using sh) and passing the command to it.

As long as your something isn't a script specifically crafted for a specific shell, there is no reason to invoke a specific shell. Just invoke any shell, by using sh.

If you have a script, that uses special syntax only available in bash, then you should set the shebang accordingly.


sh is not a shell command but a program you are calling. Your line will search for sh in your path. In most cases it will just execute /bin/sh which is in the path.

This will start a new process.

/bin/sh is not guaranteed to be bash, it is now on many systems bash but on several Unix system it is the Bourne shell. If you execute /bin/sh you can just expect a shell adhering to the POSIX standard.