You can check for the presence of shell-specific variables:
For instance, bash defines $BASH_VERSION.
Since that variable won't be defined while running in dash, you can use it to make the distinction:
[ -n "$BASH_VERSION" ] && isBash=1
Afterthought: If you wanted to avoid relying on variables (which, conceivably, could be set incorrectly), you could try to obtain the ultimate name of the shell executable running your script, by determining the invoking executable and, if it is a symlink, following it to its (ultimate) target.
The shell function getTrueShellExeName() below does that; for instance, it would return 'dash' on Ubuntu for a script run with sh (whether explicitly or via shebang #!/bin/sh), because sh is symlinked to dash there.
Note that the function's goal is twofold:
- Be portable:
- Work with all POSIX-compatible (Bourne-like) shells,
- across at least most platforms, with respect to what utilities and options are used - see caveats below.
- Work in all invocation scenarios:
- sourced (whether from a login shell or not)
- executed stand-alone, via the shebang line
- executed by being passed as a filename argument to a shell executable
- executed by having its contents piped via stdin to a shell executable
Caveats:
While the function returns the true shell binary name, bear in mind that some shells, notably bash and zsh, run in POSIX compatibility mode when invoked via a symlink named sh.
On at least one platform - macOS - sh is NOT a symlink. On macOS, it is a binary executable that, per man sh, re-execs the symlink at /private/var/select/sh, which may point to one of the following: /bin/bash, /bin/dash, or /bin/zsh. As of macOS Sequoia (15), it still points to /bin/bash by default (even though recent macOS versions now use /bin/zsh as the interactive default shell for users), and the function therefore returns bash.[1]
The function uses readlink, which, while not mandated by POSIX, is present on most modern platforms - though with differing syntax and features. Therefore, using GNU readlink's -f option to find a symlink's ultimate target is not an option.
(The only modern platform I'm personally aware of that does not have a readlink utility is HP-UX - see https://stackoverflow.com/a/24114056/45375 for a recursive-readlink implementation that should work on all POSIX platforms.)
The function uses the which utility (except in zsh, where it's a builtin), which, while not mandated by POSIX, is present on most modern platforms.
Ideally, ps -p $$ -o comm= would be sufficient to determine the path of the executable underlying the process, but that doesn't work as intended when directly executing shell scripts with shebang lines on Linux, at least when using the ps implementation from the procps-ng package, as found on Ubuntu, for instance: there, such scripts report the script's file name rather than the underlying script engine's.Tip of the hat to ferdymercury for his help.
Therefore, the content of special file /proc/$$/cmdline is parsed on Linux, whose first NUL-separated field contains the true executable path.
Example use of the function:
[ "$(getTrueShellExeName)" = 'bash' ] && isBash=1
Shell function getTrueShellExeName():
getTrueShellExeName() {
local trueExe nextTarget 2>/dev/null # ignore error in shells without `local`
# Determine the shell executable filename.
if [ -r /proc/$$/cmdline ]; then
trueExe=$(cut -d '' -f1 /proc/$$/cmdline) || return 1
else
trueExe=$(ps -p $$ -o comm=) || return 1
fi
# Strip a leading "-", as added e.g. by macOS for login shells.
[ "${trueExe#-}" = "$trueExe" ] || trueExe=${trueExe#-}
# Determine full executable path.
[ "${trueExe#/}" != "$trueExe" ] || trueExe=$([ -n "$ZSH_VERSION" ] && which -p "$trueExe" || which "$trueExe")
# If the executable is a symlink, resolve it to its *ultimate*
# target.
while nextTarget=$(readlink "$trueExe"); do trueExe=$nextTarget; done
# on MacOS, sh is not a symlink, but an executable that
# re-execs /private/var/select/sh, so resolve it
if [ "$(basename $trueExe)" = "sh" -a -L "/private/var/select/sh" ]; then
trueExe="/private/var/select/sh";
while nextTarget=$(readlink "$trueExe"); do trueExe=$nextTarget; done
fi
# Output the executable name only.
printf '%s\n' "$(basename "$trueExe")"
}
[1] If symlink /private/var/select/sh doesn't exist "or does not point to a valid shell, sh will use one of the supported shells", per man sh; in other words: it is then unspecified which shell among bash, dash, and zsh is used; in practice, as of macOS Sequoia (15), it falls back to /bin/bash. Note that while macOS comes with said symlink, it is possibly to modify or even delete it via sudo, though doing so has far-reaching ramifications, given that different shells may behave subtly differently even when run in POSIX-compatibility mode.
#!/bin/bashwill make it be executed with/bin/bashwhen called with./script.sh. Also, good to see Pink Floyd around :)bash's features are a superset ofdash's features;dashis smaller and faster, but is mostly limited to POSIX features - see en.wikipedia.org/wiki/Debian_Almquist_shell