I'm using macOS, and since it ships with Bash 3.2, I've upgraded to 4.4 via homebrew. I've configured my terminal to use the newer version by setting its startup command to:

/usr/local/bin/bash -l

I was recently playing around with the script command to record and playback terminal sessions, and found that it was using Bash 3.2

Setting export SHELL=/usr/local/bin/bash fixes this, but I'm wondering whether it's advisable to set it. I imagine it might be inadvisable not to set it, but I'm just not sure what else makes decisions based on the SHELL env var.

  • Have you tried setting the shell to /usr/local/bin/bash in /etc/passwd? – John1024 Aug 10 '17 at 22:25
  • 1
    Is /usr/local/bin/bash in your /etc/shells? If not run this: sudo bash -c 'echo /usr/local/bin/bash >> /etc/shells'. Also just for solidarity make sure you have followed the steps in this guide for your upgrade: clubmate.fi/upgrade-to-bash-4-in-mac-os-x – Jesse_b Aug 10 '17 at 22:26
  • 2
    @Jesse_b Why would it matter that it's in /etc/shells? That file is only used by chsh, unless macOS does something unusual. – Gilles 'SO- stop being evil' Aug 10 '17 at 23:11
  • Where is /usr/local/bin compared to /usr/bin in your PATH ? – thrig Aug 10 '17 at 23:27
  • 1
    @Jesse_b Ok, that file is only used by chsh and a few other system services that are relatively uncommon these days. The point is, it's only used to check the user's current login shell. It has nothing to do with the SHELL environment variable. – Gilles 'SO- stop being evil' Aug 10 '17 at 23:45

The conventional meaning of the SHELL environment variable is the user's favorite interactive shell. There is no obligation that it has any particular syntax (it doesn't have to be Bourne-like), that it supports any particular command line syntax (such as -c), or that it has anything to do with the login shell (applications that use SHELL typically default to the login shell if SHELL is unsed). It's mostly used by terminal emulators as the program to run by default.

If you log in in text mode (on a text console or over SSH), what you get is the login shell listed in the user database (e.g. /etc/passwd). When you open a terminal in a GUI environment, you get the shell specified by the SHELL environment variable if the variable is set. If you want the same shell for text mode logins, either change your login shell with chsh, or make your login shell switch to your favorite shell with exec.

Occasionally you might encounter a program that uses $SHELL -c instead of sh -c to execute code in sh syntax. But that's pretty rare. It's technically allowed by POSIX, but it would violate historical usage. In practice, setting SHELL is safe. I've had my login shell set to /bin/sh and SHELL set to /path/to/zsh on most machines for about two decades.

  • When you say: "you get the shell specified by the SHELL environment variable if the variable is set" How does that environment variable get set? (Pretty sure the passwd file is what sets it. -- not in mac though) – Jesse_b Aug 11 '17 at 0:05
  • @Jesse_b It gets set because the user sets it, typically in ~/.profile. A few programs (Screen, at least) set it (to the login shell) if it's unset. – Gilles 'SO- stop being evil' Aug 11 '17 at 0:14
  • I disagree with that. You do not need to set SHELL in any of your profile files and it will still always be set by the /etc/passwd file. – Jesse_b Aug 11 '17 at 0:16
  • 1
    @Jesse_b True, at least on Linux you get SHELL set to your login shell when you log in. That doesn't make much difference from having it unset. – Gilles 'SO- stop being evil' Aug 11 '17 at 0:29
  • 1
    @Jesse_b Debian and Ubuntu have dash as /bin/sh, but /bin/bash as the default login shell. No Linux system has ever come up with an actual Bourne shell. – Gilles 'SO- stop being evil' Aug 11 '17 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.