0

According to O'Reilly Linux pocket guide by Daniel J. Barrett, the command echo $shell should print out the current shell in use.

When I ran that command from the terminal I got a blank line. My understanding is that there should be a shell running if you have a terminal open, therefore I'm confused why it would print out blank.

raven# echo $shell

raven#

  • 3
    Welcome to the site. While it doesn't affect readability too much in this particular case, please don't post screenshots of console output. They are often difficult to read, the content will not show up in search engine results, and contributors trying to help will have to type-copy content when trying to analyze/reproduce your problem. Instead, paste it into the question using code formatting. – AdminBee Sep 02 '21 at 08:08

2 Answers2

3

Only the csh / tcsh shells set the $shell variable. So you'd only get the path of a shell with that echo $shell command if run from one those shells¹, maybe you found that in a section of that book that talks about those.

login or other login managers set the SHELL environment variable to your login shell (the shell mentioned in the passwd database for your account), which shells will invariably map to their corresponding $SHELL variable and it's the environment variable you want to set to your preferred shell for things like terminal emulators or vi to start it instead of your login shell.

But that's not necessarily the shell that is currently in use.

It would be the case if you have started your terminal emulator in an environment where that $SHELL variable had not been modified since login and your terminal emulator is not otherwise configured to start a different shell (or other command) from the one stored in $SHELL, and a different shell has not been started or the $SHELL variable modified as part of your $SHELL shell startup sequence.

There is no shell variable that will tell you which shell is in use regardless of the shell though beside (t)csh's $shell, there's bash's $BASH or $BASH_VERSION/$BASH_VERSINFO, zsh's $ZSH_VERSION, $KSH_VERSION in some ksh implementations, ${.sh.version} in ksh93, $YASH_VERSION in yash which can help you tell which shell you're running.

On Linux, you can also do readlink "/proc/$$/exe" to get the path of the shell interpreter with those shells where $$ expands to the pid of the process that executed the interpreter (Bourne-like, csh-like; see $fish_pid in fish, $pid in rc-like shells).

More portably, sh -c 'ps -o comm= -p "$PPID"' will give you the name of the parent process of sh, which should be running the shell currently in use, and the name of that process should be derived from the basename of the shell interpreter executable. All common shells should interpret that code the same, an all systems in scope of this site should have a sh and ps commands that can understand that.

To print the contents of a variable followed by a newline in Bourne-like shells, the syntax is:

printf '%s\n' "$SHELL"

Or in csh:

printf '%s\n' $SHELL:q

Or in rc-like or fish or zsh shells:

printf '%s\n' $SHELL

not echo $SHELL (though when you don't know which shell you're running, that's probably a good enough approximation that should work in most shells in most cases).

To know which is your login shell, as stored in the passwd database, on many systems, you can do (Bourne syntax):

getent -- passwd "$LOGNAME" | cut -d: -f7

Or on systems without a getent utility:

perl -le 'print((getpwnam getlogin)[8])'

¹ but only if $shell doesn't contain wildcard characters, and could also be mangled by echo which can't output arbitrary data

2

Shell variable names are case-sensitive. The convention is that environment variables have all-uppercase names. You should type

echo $SHELL

instead. As noted by @StephenKitt, this is also the command actually stated in the book.

As a general note, using echo to print the content of shell variables is becoming discouraged in favor of printf, as also noted in the anwer by @Stéphane Chazelas.

AdminBee
  • 22,803
  • Thank you. I will make note to be more aware of lower and upper case. – Dmagamon Sep 02 '21 at 08:48
  • @Dmagamon You're welcome. It is one of the nice shell pitfalls. One consequence is the recommendation that it is advisable that you don't choose all-uppercase names for shell variables in your own scripts unless you want to export them as environment variables, in order to prevent accidentally overwriting existing shell variables (which might make your scripts stop working correctly). – AdminBee Sep 02 '21 at 08:50
  • 1
    @StephenKitt, but that book is still wrong (beside the missing quotes and usage of echo) if it claims that gives you the shell currently in use without stating the conditions under which that might be true. – Stéphane Chazelas Sep 02 '21 at 10:02
  • 1
    @StéphaneChazelas yes, it is wrong — it says “To see if you’re running bash, type: echo $SHELL”... – Stephen Kitt Sep 02 '21 at 10:14
  • It's wronger than that. If I am in bash, and run ksh at the command line, ksh inherits SHELL in its environment, so it then claims to be /bin/bash too. – Paul_Pedant Sep 02 '21 at 16:15