I was reading about the differences between /bin/sh and /bin/bash and came across this interesting question/answer: here
I made a comment to the answer asking this same question in my title to which he replied with an updated answer:
How can you find out what /bin/sh points to on your system?
The complication is that /bin/sh could be a symbolic link or a hard link. If it's a symbolic link, a portable way to resolve it is:
> % file -h /bin/sh
> /bin/sh: symbolic link to bash
If it's a hard link, try
> % find -L /bin -samefile /bin/sh
> /bin/sh
> /bin/bash
I tried this and had trouble so I thought I would make a separate question.
My results from the linked answer:
>file -h /bin/sh
>/bin/sh: executable (RISC System/6000) or object module
>find -L /bin -samefile /bin/sh
>find: bad option -samefile
What am I missing? I'm running AIX:
>oslevel -s
7100-03-03-1415
Since you are only searching through
bin
anyway, you can bypassfind
entirely and just check ifsh
andbash
are hard links to the same file:OR
This is not as reliable as running
find
on all the possibilities, but it's a good start. While AIXfind
doesn't support-samefile
, it does support-exec
, which can be combined with the command above to simulate the same functionality:As the answer you commented on says,
-samefile
isn't a standard feature offind
. Neither is-inum
, which searches based on an explicitly given inode number. But if yourfind
supports that, use it.POSIX
ls
supports-i
, which prints the inode numbers. Iffind
doesn't have-inum
, all you need to do is go through all plausible files/bin/sh
could be a hard link with...Though before that, you could check if the file even has any other hard links,
ls -l
is required to show the number of links:Of course, there's no requirement for
/bin/sh
to be linked to anything. It could be just another program. (Or even an identical copy of some of other file, though I don't think that's very likely.)Check for GNU Bash
I'm going to answer your question in a different way, because it's actually simpler to find out if sh is GNU Bash (or something else that responds to a
--version
flag) than it is to chase inodes. There's also the edge case where the shell is renamed rather than linked, in which case mapping links won't really help you find an answer.For example, to interrogate /bin/sh on macOS:
Alternatively, you can grep (or similar) for the string
bash
to capture the exit status. For example:Of course, /bin/sh could be some other shell besides bash or the original bourne shell, but that's outside the scope of your original question. However, many shells such as ksh and tcsh also support the version flag, so judicious use of a
case
statement could help you extend the test to determine exactly which shell binary /bin/sh really is.You can also do
readlink -f /bin/sh
. For me it points to/bin/dash
.If you need to programatically test if they are the same, you can use
stat
to query theinode
of/bin/sh
and compare with theinode
of/bin/bash
.If you just need to see with your eyes if they are the same run the
stat
commands and see if they return the sameinode
number.I can not really see the point of this exercise.
/bin/sh
is a POSIX shell andbash
has extensions to the POSIX standard that/bin/sh
does not support.If you are writing a POSIX shell script, use
/bin/sh
. If you are writing abash
shell script, usebash
.Do not try to write a
bash
script that will be executed with/bin/sh
, and don't try to programatically try to determine the abilities of the current shell based on what/bin/sh
(or the current interpreter) is linked to. Instead, make the script executable and use the correct#!
-line for the script.