Linux Shells and Types Explained

This tutorial explains what a Linux shell is and how to view the default shell, list all available shells, and change the default shell temporarily and permanently.

What is a Linux Shell?

Linux shell is a command language interpreter. It provides a way to execute commands, create script files, run programs, work with Linux file-system, compile computer code, and manage the computer. In simple words, the Linux shell is a command-line interface that allows us to interact with the host Linux system via commands.

To type a command, the shell provides a command prompt. Whatever text we type at the command prompt, after hitting the Enter key, the shell interprets that text. If the typed text is a valid command, the shell executes that command and displays the output. If the typed text is not a valid command, the shell displays the "Command not found" error message.

Shell derivation

The first shell was developed at Bell Labs by S. R. Bourne for early versions of UNIX. It is known as the Bourne Shell or Sh. Since this was the first shell and used as the default shell for a long time, several features and functions of UNIX were developed around this shell.

All UNIX systems need this shell to function correctly. Since Linux is derived from UNIX, it also needs the sh shell to function properly. But, instead of using the original sh shell, Linux redirects all features and functions that use the original sh shell to its default shell.

On the UNIX file system, the default location of the sh shell is /bin/sh. Linux links this location to the location of its default shell. For example, if the bash is the default shell on a Linux system, then the /bin/sh will be linked to the location of the bash shell.

On a standard Linux (or UNIX) system, a variety of shells are available. Each shell has its distinct properties and default environment variables. Each shell contains a collection of built-in commands and additional tools that allow us to interact with the host system.

Usually, a Linux distributor picks a shell that meets its requirements and customizes the shell for its audiences, and sets the shell as the default shell on its distribution. No matter which shell a distributor chooses, they all are derived from the original Bourne shell.

Since all shells are derived from the same Bourne shell, they all share several features and functions. These similar features and functions make learning different shell easier. Once you learned one shell, you can easily learn and use other shells.

Popular Linux shells

The most popular Linux and UNIX shells are the following.

The ksh (Korn Sh) shell

This shell was designed and developed by David G. Korn. This shell is popular among UNIX System V users. Built-in arithmetic, string manipulation, and C-like arrays are some common features of this shell.

The bash (Bourne-Again Sh) shell

This shell is the improved version of the original Bourne shell. This shell is used as the standard shell in Linux. Many popular distributions such as Red Hat, Ubuntu, and Debian use this shell as the default shell. This shell is customized for general purposes and for all users.

The csh shell

This shell was developed by William Joy. This shell is popular among BSD UNIX users. This shell is mainly customized for the programmers. It provides an environment that the C programming language usages. Command-line editing, job control, spelling correction, C like arrays and syntax, and filename completion are some common features of this shell. The tcsh shell is an improved version of the csh.

The dash shell

This is a bare-bones and POSIX-compliant implementation of the sh shell usually used only at system boot time. This shell is faster than the bash shell. In earlier versions of Ubuntu, this shell was the default shell.

Shell types

You can classify shells into two major types. Shells that are optimized for all users and shells that are optimized for programmers. Shells such as sh shell, bash shell, k shell, and dash shell are designed for general purposes and all users. Shells such as c shell and tcsh shell are designed for programmers.

Which shell should I use?

If you are a common Linux user or a system administrator, you should choose a shell that is optimized for general purposes such as the bash shell. Or, if you are a programmer, and use Linux mainly for programming purposes, you may consider choosing a shell that is mainly optimized for programming related tasks such as the tsch shell.

Managing shells

Usually, Linux distributors include all popular shells in their distributions and customize one shell that meets their requirement and set that shell as the default shell. This flexibility allows users to change the default shell. Users can use the default shell or can set any shell as the default shell from all available shells.

Viewing the current shell and listing all available shells

To view the current shell, use the echo $0 command.

$echo $0

To list all available shells, either use the cat /etc/shells command or use the chsh –l command. Both commands list all available shells on the system.

$cat /etc/shells
$chsh -l

The following image shows the output of the above commands.

viewing the default shell and listing all available shells

Changing the current shell temporarily

To change the current shell temporarily, use the following command.

$exec [path of the new shell]

For example, the following command sets the current shell to the C shell.

$exec /bin/csh

This command changes the current shell in the current session only. It means, the current shell will be set to the default shell again when the user will terminate the current session. Let's understand this through an example.

View the current shell and change it to the c shell. Verify that the current shell is changed to the c shell. Terminate the current session and start a new session. In the new session, view the current shell again. If the current shell is set to the default shell again, it verifies that the last change was temporary.

The following image shows this exercise with the output.

changing shell temporarily

Changing the default shell permanently

To change the default shell permanently, use the following command.

$chsh -s [path of the new shell]

For example, to set the default shell to the c shell permanently, use the following command.

$chsh -s /bin/csh

This command changes the default shell in the configuration file that sets the user profile when the user login. Since Linux reads the user's configuration file at the time of user login, this change only occurs when the user login the next time. Let's understand this through an example.

View the current shell and set it to the c shell permanently. Check the current shell again before terminating the current session. The current shell must not change yet. Now terminate the current session and login again and check the current shell. If you get the c shell, it verifies that the default shell has been changed to the c shell.

The following image shows this exercise with the output.

changing shell permanently

That's all for this tutorial. If you like this tutorial, please don't forget to share it with friends through your favorite social network.

Advertisements

ComputerNetworkingNotes Linux Basic tutorials Linux Shells and Types Explained