How to check if a specific shell is in use in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to determine which shell is currently in use in a Linux environment. You will explore different methods to check the active shell, including using the echo $SHELL command to display the current session's shell, examining the /etc/passwd file to verify a user's default login shell, and inspecting running processes with the ps command to identify the shell process.

By completing these steps, you will gain practical skills in identifying and understanding the shell environment, which is fundamental for effective command-line interaction and scripting in Linux.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/TextProcessingGroup(["Text Processing"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux(("Linux")) -.-> linux/UserandGroupManagementGroup(["User and Group Management"]) linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/TextProcessingGroup -.-> linux/grep("Pattern Searching") linux/SystemInformationandMonitoringGroup -.-> linux/ps("Process Displaying") linux/UserandGroupManagementGroup -.-> linux/env("Environment Managing") subgraph Lab Skills linux/echo -.-> lab-558763{{"How to check if a specific shell is in use in Linux"}} linux/cat -.-> lab-558763{{"How to check if a specific shell is in use in Linux"}} linux/grep -.-> lab-558763{{"How to check if a specific shell is in use in Linux"}} linux/ps -.-> lab-558763{{"How to check if a specific shell is in use in Linux"}} linux/env -.-> lab-558763{{"How to check if a specific shell is in use in Linux"}} end

Check current shell with echo $SHELL

In this step, you'll learn how to identify the shell you are currently using in the terminal. The shell is the command-line interpreter that processes your commands. Different shells have different features and syntax, although many basic commands work the same across them.

The LabEx environment uses zsh as the default shell. You can verify this by checking the value of the $SHELL environment variable. Environment variables are dynamic values that affect the behavior of processes on the computer. The $SHELL variable specifically stores the path to the user's default shell.

To display the value of the $SHELL variable, use the echo command followed by the variable name prefixed with a dollar sign ($).

Type the following command in your terminal and press Enter:

echo $SHELL

You should see output similar to this, indicating that your current shell is zsh:

/usr/bin/zsh

This confirms that the terminal is currently running the zsh shell, which is the default configuration for your LabEx environment. Understanding which shell you are using is important as you progress in your Linux journey, as some advanced features or scripts might be specific to a particular shell.

Click Continue to proceed to the next step.

Verify user shell in /etc/passwd

In the previous step, you used echo $SHELL to see the shell you are currently using. This is the shell that was launched for your current terminal session. However, the system also keeps a record of each user's default login shell in a configuration file.

This configuration file is /etc/passwd. It contains information about all the users on the system. Each line in /etc/passwd represents a user account and is divided into fields separated by colons (:). The last field on each line specifies the user's default login shell.

To view the contents of /etc/passwd, you can use the cat command. Since we are only interested in the line for the labex user, we can combine cat with the grep command to filter the output. grep is a powerful tool for searching text patterns in files.

Type the following command in your terminal and press Enter:

cat /etc/passwd | grep labex

Let's break down this command:

  • cat /etc/passwd: This command reads the content of the /etc/passwd file and prints it to the standard output.
  • |: This is a pipe. It takes the output of the command on the left (cat /etc/passwd) and sends it as input to the command on the right (grep labex).
  • grep labex: This command searches the input it receives for lines containing the string "labex" and prints those lines.

You should see a single line of output similar to this:

labex:x:5000:5000:LabEx user,,,:/home/labex:/usr/bin/zsh

This line contains several pieces of information about the labex user, separated by colons. The fields are (in order):

  1. Username (labex)
  2. Password (represented by x, the actual password hash is stored elsewhere for security)
  3. User ID (UID) (5000)
  4. Group ID (GID) (5000)
  5. User information (GECOS field) (LabEx user,,,)
  6. Home directory (/home/labex)
  7. Default login shell (/usr/bin/zsh)

The last field, /usr/bin/zsh, confirms that the default login shell for the labex user is indeed zsh, matching what you saw with echo $SHELL.

Click Continue to move on.

Inspect shell process with ps

In the previous steps, you identified your current shell using echo $SHELL and verified your default login shell in /etc/passwd. Now, let's look at the running processes on the system to see the shell process itself.

In Linux, every running program is a process. The ps command (short for "process status") is used to view information about currently running processes.

To see the processes associated with your current terminal session, you can use the ps command with the -f option, which provides a full-format listing.

Type the following command in your terminal and press Enter:

ps -f

You should see output similar to this:

UID          PID    PPID  C STIME TTY          TIME CMD
labex       XXXX    XXXX  0 HH:MM pts/0    00:00:00 zsh
labex       YYYY    XXXX  0 HH:MM pts/0    00:00:00 ps -f

Let's look at the columns:

  • UID: The user ID of the process owner (labex).
  • PID: The Process ID, a unique number for each process.
  • PPID: The Parent Process ID. This is the PID of the process that started this one.
  • C: CPU utilization.
  • STIME: The time the process started.
  • TTY: The terminal associated with the process (pts/0 indicates a pseudo-terminal, like the one you're using).
  • TIME: The cumulative CPU time used by the process.
  • CMD: The command that started the process.

In the output, you should see a line with CMD as zsh. This line represents the zsh shell process that is running your current terminal session. You will also see a line for the ps -f command itself, as it is also a running process when you execute it.

This step shows you how to use ps to inspect running processes and confirm that your shell is indeed running as a process on the system.

Click Continue to complete this lab.

Summary

In this lab, you learned how to check which shell you are currently using in Linux. You first used the echo $SHELL command to display the path of the shell running in your current terminal session. This showed that the LabEx environment defaults to zsh.

You then explored the /etc/passwd file, which stores information about system users, including their default login shell. By examining the last field of your user's entry in this file, you can verify the shell assigned to your account upon login. These methods provide different ways to determine the shell in use, whether for the current session or as the user's default.