0

What's the difference in running the bash interpreter using the bash command itself vs sh command, where sh is also a symlink to bash?

The main difference I found is that, kill command behaves differently in sh. In sh, kill command wouldn't accept signal name, it would accept only signal numbers.

But in both sh vs bash run, kill command is a shell builtin. Seems like bash is behaving differently if invoked using sh.

Is there any documentation regarding this behaviour?


More info is given below:

script.sh

#!/bin/bash

some_bg_program()
{
    sleep 10
}

some_bg_program&
pid=$!
kill -SIGTERM $pid

Output for running as bash script.sh and sh script.sh

user@machine/tmp$ sh script.sh
script.sh: line 8: kill: SIGTERM: invalid signal specification
user@machine/tmp$ bash script.sh

(bash script.sh ran successfully and didn't throw any error).

Info regarding bash and sh commands:

user@machine/tmp$ which bash
/usr/bin/bash

user@machine/tmp$ which sh
/usr/bin/sh

user@machine/tmp$ ls -l /usr/bin/sh
lrwxrwxrwx. 1 root root 4 Aug  4  2020 /usr/bin/sh -> bash        # You can see the symlink here

user@machine~>ls -l /usr/bin/bash
-rwxr-xr-x. 1 root root 964608 Oct 30  2018 /usr/bin/bash

user@machine/tmp$ type sh
sh is hashed (/usr/bin/sh)
user@machine/tmp$ type bash
bash is hashed (/usr/bin/bash)

Info regarding the kill command in bash/sh

user@machine/tmp$ cat script.sh
#!/bin/bash
which kill
type kill

user@machine/tmp$ sh script.sh
/usr/bin/kill
kill is a shell builtin

sas@172.20.50.161/tmp>bash script.sh
/usr/bin/kill
kill is a shell builtin

So kill is a shell builtin in both of the cases

and finally, bash info:

$ bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
$ sh --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)

Thanks

3

When the bash shell is started with the name sh, it will automatically be running in POSIX mode, as if started with the --posix option.

The "SEE ALSO" section of the bash manual refers to http://tiswww.case.edu/~chet/bash/POSIX for a description of the shell's POSIX mode. That web page says, relating to your issue with the built-in kill utility:

The following list is what's changed when 'POSIX mode' is in effect:

[...]

  1. The 'kill' builtin does not accept signal names with a 'SIG' prefix.

So yes, the kill built-in utility does not accept signal names prefixed by SIG in POSIX mode. You may want to use the signal names without the SIG prefix, i.e.

kill -INT "$pid"

or

kill -s INT "$pid"

This is also how the POSIX specification for the kill utility says it should be used.

Note that kill -TERM "$pid" would be the same as kill "$pid" as the TERM signal is the default signal that kill sends.

I would not use the signal numbers (with the exception for the 0 signal) as these are difficult to remember and, apart from a short list, differs between Unices.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.