1

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.

6
  • What about [[ ! -z "$SUDO_COMMAND" ]] && echo $SUDO_COMMAND || echo $SHELL ? Jan 13, 2022 at 22:12
  • That works, thanks. However, [[ ! -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.
    – WGroleau
    Jan 13, 2022 at 23:28
  • For what it's worth, if you want sudo to always use zsh, do sudo chsh -s /bin/zsh root
    – WGroleau
    Jan 13, 2022 at 23:43
  • I guess a slightly easier way is env | grep COM or just env and look for SUDO_COMMAND.
    – WGroleau
    Jan 13, 2022 at 23:47
  • A number of other options here: stackoverflow.com/questions/3327013/…. I've had some luck with ps -fp $$, but there are times when that won't work.
    – Gairfowl
    Jan 14, 2022 at 12:44

2 Answers 2

1

If 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.

5
  • Also works for fish - and sort of for xonsh
    – mmmmmm
    Jan 20, 2022 at 8:49
  • 1
    Even simpler than the previous answer. Plus this one will also work in a script. In a script, $0 gives the name of the script and not the shell. On the other hand, in a script, this one would require parsing stderr to get the shell name.
    – WGroleau
    Jan 20, 2022 at 16:29
  • @WGroleau: Querying the parameter $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
  • The method depends on the error message. Doesn't that message get sent to stderr? The bang line of a script might determine the shell, or it could specify perl. Moreover, the parameters passed to the script are parsed by the shell. However, the question was specifically concerning an interactive shell, and therefore, I accepted this answer.
    – WGroleau
    Jan 21, 2022 at 16:35
  • Anyway, your answer did not say anything about $0. The other one did. That's why I pointed out that your answer, by NOT using $0, might also work in a script, even though that was not the question I asked.
    – WGroleau
    Jan 21, 2022 at 16:41
5

To get the current shell, you can run:

echo $0


To get the the default shell, you can run:

echo $SHELL
3
  • 1
    Coments showed another way, but this is definitely the simplest so far.
    – WGroleau
    Jan 14, 2022 at 19:17
  • 1
    "Gairfowl" points to a lot of options, but for my question (taken literally), this is the simplest.
    – WGroleau
    Jan 14, 2022 at 19:38
  • This only works for POSIX and csh type shells
    – mmmmmm
    Jan 20, 2022 at 8:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .