bash4.3 # pwd
bash4.3 # ll sh
lrwxrwxrwx. 1   root    root    4   May 17 22:22 sh -> bash
bash4.3 # ll bash
-rwxr-xr-x. 1   root    root    1072056 May 17 22:22 bash
bash4.3 # bash
bash4.3 # sh

My OS is fedora 24 (Default GNOME version).

From the example, we can know: under /bin, bash is a binary executable; sh is a soft link to bash.

So, to my knowledge, type bash and press enter should effect the same result exactly as type sh and press enter.

When I type bash and press enter, I get [root@localhost bin]# as expected.

However, if I type sh and press enter, I surprisingly get sh-4.3#.

What's the cause?

  • Text which could quite easily be copy-pasted into a SE editor as an image is quite annoying, so please don't do this. Instead, select the text in your terminal and press CTRL-SHIFT-C.
    – cat
    Jul 20, 2016 at 13:50

1 Answer 1


That is a documented feature.

If you run bash via a symlink named sh, bash will start in sh compatibility mode.

From man bash:

If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well. When invoked as an interactive login shell, or a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order. The --noprofile option may be used to inhibit this behavior. When invoked as an interactive shell with the name sh, bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect. A non-interactive shell invoked with the name sh does not attempt to read any other startup files. When invoked as sh, bash enters posix mode after the startup files are read.

How does a program know what name was used to start it?

If it is a c program, it can inspect argv[0]. If it is a shell or perl script, it can inspect $0.

As an example, let's consider this simple shell script:

$ cat utc
case "${0##*/}" in
        utc) date -u ;;
        et) TZ=US/Eastern date ;;

$0 is the name that the script was called by. ${0##*/} is the name that the script was called with any directory names removed.

Let's create this symlink:

ln -s utc et

So, utc and et both run the same executable but they provide different results. When run as utc, it outputs universal time. When run as et, is outputs US Eastern time. For example:

$ utc
Wed Jul 20 18:14:18 UTC 2016
$ et
Wed Jul 20 14:14:20 EDT 2016
  • 4
    Might want to add how this is possible, since the OP expects the calls to be identical. (argv[0], obvs) Jul 20, 2016 at 11:58
  • @LightnessRacesinOrbit Good idea. I added an example.
    – John1024
    Jul 20, 2016 at 18:18
  • Perfect now. :) Jul 21, 2016 at 10:47

You must log in to answer this question.

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