Finding out the default shell of a user within a shell script
The environment variable, SHELL
would always expands to the default login shell of the invoking user (gets the value from /etc/passwd
).
For any other given user, you need to do some processing with /etc/passwd
, here is a simple awk
snippet:
awk -F: -v user="foobar" '$1 == user {print $NF}' /etc/passwd
Replace foobar
with the actual username.
If you have ldap
(or something similar in place), use getent
to get the database instead of directly parsing /etc/passwd
:
getent passwd | awk -F: -v user="foobar" '$1 == user {print $NF}'
or cleanest approach, let getent
do the parsing (thanks to @Kusalananda):
getent passwd foobar | awk -F: '{print $NF}'
$ finger $USER|grep -oP 'Shell: \K.*'
/bin/mksh
Since getent
isn't a standard command on MacOS, you may wish to use a lower level getpwuid
call that will consult the naming services the machine is configured for. This may require calling out to perl
or python
, which are pretty common across most Unix-like platforms
eg this will return the shell for the current user:
perl -e '@x=getpwuid($<); print $x[8]'