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:
On at least one platform - macOS - sh
is NOT a symlink, even though it is effectively bash
. There, the function would return 'sh'
in a script run with sh
.
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://mcmap.net/q/109951/-relative-paths-based-on-file-location-instead-of-current-working-directory-duplicate 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
# Output the executable name only.
printf '%s\n' "$(basename "$trueExe")"
}
#!/bin/bash
will make it be executed with/bin/bash
when called with./script.sh
. Also, good to see Pink Floyd around :) – Costplusbash
's features are a superset ofdash
's features;dash
is smaller and faster, but is mostly limited to POSIX features - see en.wikipedia.org/wiki/Debian_Almquist_shell – Copyreader