Reputation: 5686
We can run something like chsh -s /usr/local/bin/zsh
to set a new default shell. Is there a command we can run to know what that shell is?
I don’t mean having a terminal open and running a command to know which shell we’re in. I mean like in the example above, if I’m in a terminal with /bin/bash
open, what should I run to get /usr/local/bin/zsh
if it’s the current default shell?
Upvotes: 51
Views: 53556
Reputation: 195049
If you want to get the default shell of a user, you could grep file /etc/passwd
. like:
grep "$USER" /etc/passwd
# kent:x:1000:1000::/home/kent:/bin/zsh
telling me that the current user (kent) has the default shell /bin/zsh
.
If you just want to catch the shell part:
awk -F: -v u="$USER" 'u==$1&&$0=$NF' /etc/passwd
# /bin/zsh
If you want to get the default shell of other user, just replace the $USER part.
Upvotes: 1
Reputation: 726
For macOS:
dscl . -read /Users/username UserShell
For the current macOS user:
dscl . -read ~/ UserShell
To parse the path inline using sed
:
dscl . -read ~/ UserShell | sed 's/UserShell: //'
Using $SHELL
will report the current login shell, not the default login shell. In certain cases, these are not the same. For example, when working in an IDE such as Visual Studio Code which opens an integrated terminal without consulting the default shell.
In addition, as pointed out by Martin C. Martin, $SHELL
is a constant that will not change after chsh
changes the default login shell.
Upvotes: 52
Reputation: 129
In OS X, using the command env | grep -i 'SHELL'
produces an output such as: SHELL=/bin/sh
(as root, however regular users tend to have /bin/bash as default shell) with a little parsing, the path the shell (and thus the shell itself) could be easily identified and extracted from there..
Upvotes: 1
Reputation: 5457
You can grep in the /etc/passwd file for current username, and use cut to extract the appropriate column of information:
grep ^$(id -un): /etc/passwd | cut -d : -f 7-
$(id -un)
is a safer than $USER
to get user name. Using ^
in front of user name and :
after makes sure you don't get a false match if your user name is a sub section of someone else user name.
$SHELL
can also be used, as suggested. However it won't work if chsh
was used in current shell, as the variable is not updated. Also the variable is not protected against being changed, so it can theoretically be set to something completely different.
Update to attempt an OS X compatible solution. Probably not optimal regexp:
grep ^.*:.*:$(id -u): /etc/passwd | cut -d : -f 7-
This is based on user id's. If the whole user entry is missing, not only user name, then osx must store this somewhere else.
Upvotes: 2