If I do sudo zsh
, I can tell I am in zsh by examining ps -ef
Is there an easier way? echo $SHELL
says it's /bin/sh
, because $SHELL is always the default shell, not necessarily the shell that's actually running.
Ask Different is a question and answer site for power users of Apple hardware and software. It only takes a minute to sign up.
Sign up to join this communityIf I happen to be on some interactive shell, and just want to quickly verify what kind of shell it is, I usually do a
)
If I'm in zsh, I get
zsh: parse error near `)'
, bash says
bash: syntax error near unexpected token `)'
and dash and ash also reveal their name in this way. I guess that this approach would also work with csh, tcsh and ksh.
$0
is not related to stderr. However $0
contains whatever the calling process passes as name to the invoked program. For instance, login shells are sometimes invoked under a different name (-zsh
instead of zsh
is frequently used), and if you invoke a shell via a symlink, $0
contains that symlink path. Aside from this, I don't quite see why you want to query this in a script. If you write a script, you know what language you are using. It's like, if you write a Fortran program, you don't want to query whether your program is Fortran or Ada.
Jan 21, 2022 at 7:44
To get the current shell, you can run:
echo $0
To get the the default shell, you can run:
echo $SHELL
[[ ! -z "$SUDO_COMMAND" ]] && echo $SUDO_COMMAND || echo $SHELL
?[[ ! -z "$SUDO_COMMAND" ]] && echo $SUDO_COMMAND
also works. If the first part ([[ … ]]
fails, will it exit and do nothing? Or will it do the third part which will say /bin/sh (which is wrong). Although it works, it's not an easier way. Also, if I'm running a script that doesn't have a bang line (perl?) or a compiled executable, then $SUDO_COMMAND will be the name of the script or executable. I suppose then, it's not in any shell, but if it calls something else, it might want to check the shell.sudo chsh -s /bin/zsh root
env | grep COM
or justenv
and look for SUDO_COMMAND.ps -fp $$
, but there are times when that won't work.