61

What is the *nix command to view a user's default login shell?

I can change the default login shell with chsh, but I don't know how to get what is the user's default shell.

Pseudocode

$ get-shell
/usr/bin/zsh
k107
  • 15,882
  • 11
  • 61
  • 59

5 Answers5

78

The canonical way to query the /etc/passwd file for this information is with getent. You can parse getent output with standard tools such as cut to extract the user's login shell. For example:

$ getent passwd $LOGNAME | cut -d: -f7
/bin/bash
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • getent will get you output ala /etc/passwd. the finger command below makes that human readable for those not used to make-up of the passwd file. getent is the more appropriate tool though. – John Kloian Jun 02 '14 at 18:03
  • Well, the questioner makes clear that they want a way to just see the shell, because grepping /etc/passwd will still show you the shell and other data, but getent is the most fundamental way to query data from the database format that such files are in. – Spencer Williams May 20 '15 at 21:03
  • 1
    It's worth noting that /etc/passwd does not always contain this information on actual multi-user sites; it may be present in an LDAP database. Unfortunately although I'm vaguely aware that PAM is really the system responsible for looking this stuff up, I don't know *how* to look it up, so I'm hoping for another answer. – Glyph Dec 19 '15 at 10:02
  • 2
    In addition to truly multi-user machines, this won't work on OS X. – Glyph Dec 19 '15 at 10:04
  • @Glyph my setup uses LDAP/PAM and those entries show up correctly with `getent passwd` - seems like it dumps /etc/passwd first and then the other sources one after another. – mbx Jun 19 '19 at 21:06
  • @mbx I believe the "passwd" entry of /etc/nsswitch.conf defines the order of the lookup. – Phil Apr 22 '21 at 18:51
24

The command is finger.

[ken@hero ~]$ finger ken
Login: ken                      Name: Kenneth Berland
Directory: /home/ken                    Shell: /bin/tcsh
On since Fri Jun 15 16:11 (PDT) on pts/0 from 70.35.47.130
   1 hour 59 minutes idle
On since Fri Jun 15 18:17 (PDT) on pts/2 from 70.35.47.130
New mail received Fri Jun 15 18:16 2012 (PDT)
     Unread since Fri Jun 15 17:05 2012 (PDT)
No Plan.
user1460011
  • 241
  • 1
  • 2
  • CentOS 6 doesn't have this command, but you can easily install it with `yum install finger`. – noun May 29 '15 at 14:22
11

The login shell is defined in /etc/passwd. So you can do:

grep username /etc/passwd
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
timos
  • 2,637
  • 18
  • 21
3

I think what you are looking for is this:

#!/bin/bash
grep "^$1" /etc/passwd | cut -d ':' -f 7

Save that as get-shell somewhere in your path (probably ~/bin) and then call it like:

get-shell userfoo
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Lucas
  • 14,227
  • 9
  • 74
  • 124
1

SHELL variable is used to represent user's current shell

echo $SHELL
nilsocket
  • 1,441
  • 9
  • 17
  • The "default login shell" the user asked for is not necessarily the same thing as their current shell. – makeworld Nov 08 '20 at 05:08
  • Actually, the SHELL variable does *not* contain the current shell. It actually contains the setting for the *user's preferred shell* (which is generally the same as their default login shell). This is, therefore, actually a good answer to the original question, and one of the simplest, and most portable (getent, for example, does not work on MacOS, but $SHELL does). – Foogod Dec 02 '20 at 22:48
  • @Foodod Well, except for that one fairly unusual circumstance where the user changes the value of their own SHELL variable. Then they'd get whatever they set it to last, not their login shell. But, yeah, that's rare enough not to worry about (unless you have to). And, if they bothered to change it, maybe they really do want that value rather than the login shell. – Phil Apr 22 '21 at 19:19