LINFO

How to Change Shells



It is a simple matter to change the current shell for any user on Linux or another Unix-like operating system. The shell for any user can be switched temporarily, or it can be changed semipermanently (i.e., made the new default).

A shell is a program that provides the traditional, text-only user interface for Unix-like operating systems. Its primary function is to read commands that are typed into a console (i.e., an all-text display mode) or terminal window (an all-text window) in a GUI (graphical user interface) and then execute (i.e., run) them.

Although bash, the default shell on Linux, is highly versatile and can be used for almost anything, each shell has its own characteristics and there might be situations in which it is preferable to use some other shell, such as ash, csh, ksh, sh or zsh. For example, the csh shell has a syntax that resembles that of the highly popular C programming language, and thus it is sometimes preferred by programmers.

Sometimes it is useful to first determine the current default shell for a user. This can be accomplished by using the grep command (which is commonly used to search for specific text within files) to read the entry for the default shell for the current user's account in /etc/passwd (the file that stores information about users). For example, the following could be used to find the default shell for a user named janis:

grep janis /etc/passwd

grep might return a line something like janis:x:501:501::/home/janis:/bin/bash, which indicates that the shell is bash.

It can also be useful to see what shells are available on a particular system. This can be accomplished by using the cat command (which is commonly employed to read files) to read the /etc/shells file, which lists all shells installed on the system, as follows:

cat /etc/shells

All that is necessary for a user to switch to a different shell (assuming that it is already installed on the system) for the current session is to type its name at the command line and then press the ENTER key. Because a shell is an executable file (i.e., a program), the current shell launches it, and it then takes over from the current shell. For example, to change from any shell to the sh shell, all that is necessary is to type the following and then press the ENTER key:

sh

Upon changing shells, a different command prompt (i.e., the short text message at the left of each command line) may be shown, depending on the previous and new shells. For example, if the original shell were bash and the new shell is sh, the command prompt would change for a user george from something like [george@localhost george]$ to something like sh-2.05b$.

To return to the original shell, or any other one for that matter, all that is necessary is to just type its name and then press the ENTER key. Thus, for example, to return to the bash shell from the sh shell (or from any other shell), all that is required is to type the word bash and press the ENTER key, i.e.,

bash

Methods for permanently changing the shell may vary according to the system. However, in general, the first step is to determine the full path, also called the absolute path, to the shell, which is its location on the system relative to the root directory (i.e., the directory that contains all other directories and which is represented by a forward slash). The full path for each shell is shown in the /etc/shells file, and in the /etc/passwd file for those shells listed in it. In the previous example, the full path to the bash shell was /bin/bash.

Once the full path to the new shell has been determined, a user can change its default shell using the chsh (i.e., change shell) command as follows:

chsh

This results in a brief dialog in which the user is prompted first for its password and then for the full path of the desired new shell.

Caution should be exercised when changing one's default shell because it is possible to make an error that only the root user (i.e., system administrator) can repair (although it should be easy for a skilled user to repair it on a home system). In particular, it is important to first test the shell temporarily in the current session and then to make certain that a valid shell name is being entered when making the permanent change.






Created June 4, 2006.
Copyright © 2006 The Linux Information Project. All Rights Reserved.