1. Overview

In the Linux operating system, knowing the available shells on a Linux machine is vital for administrators and users who require different shell environments for their work. There are several different shells available, each with its own features and capabilities. We can choose the one that best suits our needs or allows us to troubleshoot issues.

In this tutorial, we’ll explore various methods to list the available shells on a Linux machine.

2. Finding Available Shells by Viewing the /etc/shells File

To find available shells on a Linux machine, we can view the /etc/shells file. This file contains a list of paths to all the valid login shells installed on the system.

2.1. Using cat

The cat command is a simple and powerful utility in Linux used for displaying the contents of one or more files on the terminal.

We can use the cat command to display the contents of the /etc/shells file, which will include a list of available shells:

$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash

The output of the command displays a list of shells available on the system. Each shell is listed on a separate line, including popular shells like /bin/bash and /bin/sh. These shells represent different command-line interfaces that we can choose to interact with on the Linux machine.

2.2. Using grep

Another method to list available shells is by examining the /etc/shells file using the grep command. In Linux, the grep command is a powerful command-line tool for text searching and filtering.

Using grep allows us to filter and display only the lines that contain valid shell entries:

$ grep -v '^#' /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash

The command output displays a list of available shells on the system, excluding any lines that start with the # symbol. Further, the -v option in grep is used to invert the matching, showing lines that don’t match the specified pattern.

In the same way, we can use more, vi, and vim tools for listing available shells by viewing /etc/shells file in Linux.

3. Finding Available Shells by Using the chsh Command

The chsh (change shell) command allows us to change the login shell. Although it doesn’t directly list available shells, we can use it to check available shells. We’ll be running the chsh command in Fedora as it doesn’t run in Debian and Ubuntu-based Linux systems.

By running chsh along with -l options, we can list the available shells:

$ chsh -l
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash

The command displays a list of available shells supported by the system.

4. Finding Available Shells by Viewing the /etc/passwd File

The /etc/passwd file stores user account information, including the default login shell for each user. We can use cut tools to extract shell information from the file.

cut is a command-line utility that allows users to extract specific fields from a file. Let’s use cut to list the default login shell for each user from the /etc/passwd file:

$ cut -d ':' -f 7 /etc/passwd | sort -u 
/bin/bash
/bin/sync
/sbin/halt
/sbin/shutdown
/usr/sbin/nologin

The output displays a list of unique values extracted from the 7th field of the /etc/passwd file. These values represent different shell types available for users on the system: /bin/bash, /bin/sync, /sbin/halt, /sbin/shutdown, and /usr/sbin/nologin.

Here’s what the options next to the cut command mean:

  • d ‘:’: The delimiter (-d) is set to ‘:’ as the fields in /etc/passwd are separated by colons.
  • -f 7: The -f option specifies that it extracts field 7, which represents the user’s default shell, respectively.
  • sort -u: The sort command is used to sort the extracted data. The -u option ensures that only unique lines are displayed, removing any duplicates from the /etc/passwd file.

5. Conclusion

In this article, we explored various methods to list the available shells on a Linux machine.

On viewing the /etc/shells file using cat, grep, and the chsh command and viewing the /etc/passwd file with cut, we can identify the available shells on a Linux system. This allows us to select the most suitable shell and troubleshoot any issues that may arise.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.