Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was wondering if there's a way to find out the default shell of the current user within a shell script?

Use case: I am working on a script that sets an alias for a command and this alias is set within a shell script.

!# /bin/bash
alias = 'some command to set the alias'

There's a logic in the script where it tries to find the default shell of the user that executes the script and adds this alias in the respective ~/.bashrc or ~/.zshrc file

But as I am adding a shebang in the front of the script and explicitly asking it to use bash, answers posted here always return bash as expected although I am executing this script on a ZSH terminal.

Is there a way to get the shell type where the script is executed regardless of the shebang set?

I am looking for a solution that works on both Mac and all the linux based bistro.

share|improve this question
    
@StephenRauch I should have mentioned it earlier, I am looking for a method that works on both Mac and Linux based system. /etc/passwd on Mac does not contain the user information and its only consulted in a single user mode. – Kishore pandey 17 hours ago
    
What does /etc/passwd have on OSX? I had a quick look online and it appears that the shell is there, just not the username. The user ID's still there though, why don't you use that? – terdon 16 hours ago
    
I'd wonder if it's even possible to have an exhaustive list of shells. Or do all shell startup files always conform to .{name_of_shell}rc? – jamesqf 14 hours ago
up vote 3 down vote accepted
$ finger $USER|grep -oP 'Shell: \K.*'
/bin/mksh
share|improve this answer
    
Thanks it works like a charm! This is the only solution that probably works for both mac and linux based bistro. – Kishore pandey 17 hours ago
    
BTW, I edited the command a little bit to make it agnostic to all OS finger $USER | grep 'Shell:*' | cut -f3 -d ":" as the grep options -P is not supported on Mac. – Kishore pandey 17 hours ago
    
Note that finger may not be installed on all machines. It's an optional package on RedHat based systems. – Stephen Harris 16 hours ago
1  
@Kishorepandey: you can use getent|grep ^$USER:|cut -f: -f7 instead of finger, if finger was not installed by default. – Ipor Sircer 15 hours ago
    
@Kishorepandey Don't do this. Use the SHELL environment variable if it's present. Looking at the user database will give you a result (the user's login shell) but it might not be the right result (the user's favorite interactive shell) — it depends on the user's configuration. – Gilles 9 hours ago

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}'
share|improve this answer
2  
getent passwd foobar to skip having to match a particular line. – Kusalananda 17 hours ago
    
Thanks for the answer. I should have mentioned it earlier, I am looking for a method that works on both Mac and Linux based system. /etc/passwd on Mac does not contain the user information and its only consulted in a single user mode. – Kishore pandey 17 hours ago

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]'
share|improve this answer

You can get the user's default shell from the environment like this:

echo $SHELL

But not like this:

echo $0

The latter will just tell you what shell your script is currently using. So you want the first option above.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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