There is something we always use in any Linux distribution through any terminal interface—often without realizing it: the shell. In this article, I tried to briefly explain what a shell is, how it works, the difference between built-in and external commands, and the PATH variable.
What is a Shell?
In Linux, a shell is a software that acts as an interface between the user and the operating system kernel. It takes commands from the user, interprets them, and passes them to the kernel. For example, when a user wants to run the ls command, the shell takes this command and runs the ls utility.
The most popular shell used in Linux is Bash (Bourne Again Shell), but there are many alternatives available.
| Shell | Description |
|---|---|
| Bash | The most common shell. Part of the GNU project. (/bin/bash) |
| Zsh | Offers more customization and auto-completion (/bin/zsh) |
| Sh | The simplest shell (Bourne Shell), often /bin/sh is a symlink. |
| Fish | A user-friendly and modern shell |
| Tcsh / Csh | Provides C-like syntax |
Changing the Default Shell Program
Although it may vary depending on the distribution, I mentioned that most distributions come with Bash as the default shell program. For various reasons, you might want to change this default shell. You can do this in two different ways.
By Editing the /etc/passwd File
You can find out the current default shell for the active user using the echo $SHELL command or the getent passwd $USER command. After identifying the current shell, you might want to see the shells installed on your system. To do this, you can use the cat /etc/shells command to list the full paths of the available shells on your system. To change the shell, copy the path of the desired shell and paste it in place of the current shell path in the relevant line of the passwd file for the user.
Using the chsh Command
As I mentioned above, after finding the current shell and listing the shells installed on your system (you can install the desired shell if it’s not available), you can change the default shell for the current user with the command chsh -s new_shell_path.
How Does a Shell Work?
Before explaining how a shell works, we need to understand the difference between built-in and external commands, and the purpose of the PATH variable.
Built-in Commands
Built-in commands are those that are part of the shell program itself (like Bash or Zsh). These commands are not executed from separate files; they behave like internal functions of the shell. Since no new process is created, they are faster.
External Commands
External commands are not defined inside the shell. Instead, they are standalone executable files located in certain directories (usually /bin, /usr/bin, /usr/local/bin). To run these, the shell first locates the program file and then starts a new process to execute it. This is slightly slower than running built-in commands because it involves interaction with the operating system kernel and process creation.
What is PATH?
PATH is an environment variable stored on the system. It contains the directories that the shell should search for external commands. When I run the echo $PATH command on my system, I get the following output:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
These are the directories that the shell checks for commands that are not built-in.
When we enter any command from a terminal interface, the shell interprets it. If it’s a built-in command, it executes it directly. If not, it searches for the command in the directories listed in PATH.
- The user enters a command
- The shell reads and parses the command
- It searches for the command in the directories defined in
$PATH - A new process is started (fork & exec)
- The kernel executes the command
- The shell displays the output on the screen
Stay secure! 🛡️