Get user shell linux

Finding out the default shell of a user within a shell script

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.

@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.

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?

I’d wonder if it’s even possible to have an exhaustive list of shells. Or do all shell startup files always conform to .rc?

10 Answers 10

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 ' /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 ' 

or cleanest approach, let getent do the parsing (thanks to @Kusalananda):

getent passwd foobar | awk -F: '' 

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.

Use getent unless you’re never ever going to use a directory for authentication ever. If Unix doesn’t have getent you need to restate your question to find the equivalent in your particular Unix distro.

Alas the SHELL environment variable doesn’t always exist! In fact, even when the parameter exists in a shell, it might not be exported. And using getent (or finger ) works well on Linux but fails on some other systems. I’m not aware of a foolproof, maximally portable way. As a fallback I’d just iterate a few common shell path locations and, if those don’t exist, call it a day.

$ finger $USER|grep -oP 'Shell: \K.*' /bin/mksh 

Thanks it works like a charm! This is the only solution that probably works for both mac and linux based bistro.

Читайте также:  Linux edit environment variables

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.

Note that finger may not be installed on all machines. It’s an optional package on RedHat based systems.

@Kishorepandey: you can use getent|grep ^$USER:|cut -f: -f7 instead of finger, if finger was not installed by default.

@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.

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:

grep ^$USER: /etc/passwd | cut -f 7 -d : 

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

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

$SHELL is not guaranteed to be the user’s default shell either. You want getent or its macOS equivalent.

One more way to solve the above problem of finding your default shell, just for fun:

grep ^$USER: /etc/passwd | grep -Eo ':([^:]*)$' | tr -d ':' 

Brief description: find the line with the user, find the :/my/shell path at the end, delete the : char from it.

Now, let me expand on @heemayl’s answer, and explain the magical awk code he uses.

1. Just remember the simple stuff:

awk -F: -v user="foobar" '$1 == user ' /etc/passwd 

while being 100% correct, and the most «right» answer perhaps, is impossible to remember—you’d have to learn awk and then rewrite it logically from scratch each time. What is easy to remember, however, is this: the default shell for a user is the last field on the user’s line in the «/etc/passwd» file, where fields are separated by the colon : symbol, and each line in that file begins with a username.

Now, with that knowledge, just think: «ok, I need to find my username line in /etc/passwd». So, do this:

You’ll get something like this:

my_username:x:1001:1001::/home/my_username:/bin/bash 

Cool! My default shell is therefore the last field, which means whatever is after the last : symbol. It is /bin/bash . Done.

2. Explanation of this awk command:

awk -F: -v user="foobar" '$1 == user ' /etc/passwd 

awk is a pattern matching language. You can read the GNU awk, or gawk , manual here, which is a great reference: https://www.gnu.org/software/gawk/manual/gawk.html

  1. /etc/passwd at the end is the file to open and process with awk
  2. -F: says to change the «‘F’ield separator» from space (the default) to : .
  3. The -v option sets an awk ‘v’ariable you are naming user here, to «foobar»
  4. Now the actual awk program is stored between the single quotes. It’s simply $1 == user . Remember: it is its own programming language! It is based on a pattern match or boolean check type flow. It scans down the file one «record» (or line, by default) at a time. $1 is the first field in any given line, and remember, we told it that fields are separated by : , so the first field is my_username . So, $1 == user says «does the first field equal the value stored in the awk variable user «? If so, do the action specified in curly braces < >. The action is to print the value of $NF , where the internal NF variable contains the ‘N’umber of ‘F’ields for this line. So, the number of fields is 7, since :: contains 1 empty field between those two colons. Therefore, $NF is actually $7 , or the value of the 7th field, which is /bin/bash for this user. Behold, the magic of the pattern-matching language, awk .
  5. Done.
Читайте также:  Busybox при загрузке linux

3. How to configure a default shell for your user:

What if you want to set or change the default shell for a given user? Use chsh . man chsh says it is used to «change login shell». Here’s how to use it:

sudo chsh my_username --shell /path/to/my/shell 

Ex: set to use the bash shell (my preferred choice) by default:

sudo chsh my_username --shell /bin/bash 

Additional awk learning resources:

  1. See some hello world and syntax test files for awk I wrote here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/tree/master/awk
  2. Study git diffn , as an example, here. Note: git diffn is a wrapper I wrote around git diff , to show line numbers.

Источник

4 ways to check user shell in Linux

In Linux, every user has a default shell. This is the program that is run when you log in to your account. There are many different shells available, and each one has its own unique set of features. In this blog post, we will discuss three ways to check the user shell on your Linux system. We will also provide links to more information about each shell so that you can decide which one is right for you!

understanding shell in Linux

A shell is a program that provides a user interface to the operating system. It allows you to run other programs, type commands, and interact with the system. There are many different shells available for Linux, and each one has its own unique set of features.

The /etc/shells file contains a list of all the available shells on your system. This file is used by the system to determine which shell to use when a user logs in. You can use the cat command to view the contents of this file: cat /etc/shells

You should see a list of all the available shells, one per line. The first shell listed is usually the default shell for new users.

what is the default shell of a user in Linux?

The default shell of a user in Linux is the shell that is specified when the account is created. There are many different shells available for Linux, but the default shell is the Bourne Again Shell (Bash) in most cases. Bash is a powerful shell that offers a wide range of features for its users.

The Bash shell offers many features for its users, including:

Читайте также:  Linux debian set date time

what is the current shell in Linux

The current shell in Linux is the shell that is currently running. This may be different from the default shell. Users can change their shells from this one to another. For example, after the user runs command /bin/sh, the current shell for this user becomes sh. The default shell of this user is still /bin/bash.

check the default shell in Linux with grep username /etc/passwd command

the easiest way to check the user shell in Linux is using grep username /etc/passwd command. The /etc/passwd file is a text file that contains information about the users on your system. It includes the user’s name, UID, GID, and shell. You can use grep to search this file for specific information about a user.

For example, to find the shell for the user “username”, you would use the following command: grep username /etc/passwd

This command will return the line from /etc/passwd that contains information about the user “username”. The shell for this user is the last field on this line. In most cases, the shell will be listed as “/bin/bash”.

check the default shell in Linux with echo $SHELL command

The echo $SHELL command prints the value of the SHELL environment variable. This variable contains the name of the default shell. You can use this command to find out which shell is set as default for your user.

For example, to find out the shell for the user “username”, you would use the following command: echo $SHELL. In most cases, this will be “/bin/bash”.

check the current shell in Linux with ps command

To find out the current shell, you can use the “ps” command. The “ps” command is a Linux utility that displays information about processes. To find out the current shell, you can use the “-p $$” option.

This option tells ps to display information about the process with PID (Process ID) equal to $$. The $$ variable contains the PID of the current shell. For example, to find out the current shell, you would use the following command: ps -p $$

This command will return a line of output that looks something like this:
“PID TTY TIME CMD
2742 pts/0 00:00:00 bash”

The current shell is the process with PID 2742, which is “bash”.

check the current shell in Linux with echo $0 command

echo $0 is another reliable and simple method to get the current shell interpreter name on Linux or Unix-like systems.

The echo $0 command prints the value of the 0 environment variable. This variable contains the name of the shell that is currently running. For example, if you are using the Bash shell, this command will return “/bin/bash”.

To find out the current shell for the user “username”, you would use the following command: echo $0. In most cases, this will be “/bin/bash”.

If you want to find out more information about a specific shell, you can use the “man” command. For example, to learn more about the Bash shell, you can use the command “man bash”. This will display the Bash manual page, which contains a lot of information about this shell.

I hope this article has helped you to understand how to check the user shell in Linux. If you have any questions or comments, please leave them below. Thanks for reading!

Источник

Оцените статью
Adblock
detector