Customizing Your Computing Environment with Unix Commands

2. The Login Shell

2.2. Changing Your Shell

Sometimes you may want to use one of the other available shell programs rather than the default. If you have little experience using a shell program, you probably won't have a preference for which one you use. In that case, it is probably best to use the default. If you do prefer to use a different one, you can either temporarily or permanently switch to another shell program.

You can change your shell program using the Unix exec command. When the exec command is run from a terminal window, it replaces the ongoing terminal process with the command provided in the argument. By using it to execute another shell program, you will replace your current shell with the new shell program. You can issue this command directly from the command line to change your shell temporarily or add it to the end of the file named .bash_profile, located in your home ($HOME) directory, to change it permanently. 

Below is a sample session using the UNIX exec command to change the shell program to tcsh temporarily.

[username@dt-login02 ~]$ echo $SHELL           #display default shell
/bin/bash
[username@dt-login02 ~]$ echo $0               #display current shell
-bash
[username@dt-login02 ~]$ exec -l /bin/tcsh     #change shell to tcsh
[username@dt-login02 ~]$ echo $0               #display current shell
-/bin/tcsh
[username@dt-login02 ~]$ echo $SHELL           #display default shell
/bin/bash
[username@dt-login02 ~]$

In the sample session, we first check for the default shell by entering the command 'echo $SHELL' and the current shell value by entering the command 'echo $0.' We see that both are set to bash. Next, we enter the command 'exec -l /bin/tcsh' to change the shell to tcsh. Just to be sure, we then check that the shell was actually changed by entering the 'echo $0' command. We then enter the 'echo $SHELL' command and see that the default shell is still set to bash. This is what we should expect to see. The changes made with the exec command will only be in effect during the current terminal session. Therefore, your default shell will not have been changed.

If you want to change your shell to tcsh and have it be the current shell whenever you log into your HPC cluster, you need to change the value in your system startup file. This file, .bash_profile, is typically located in your home ($HOME) directory.  Add the following line:

exec -l /bin/tcsh

to the end of .bash_profile using a text editor like nano or vi. You must log out and then log back in for this change to take effect.

Icon indicating an exerciseExercises

Note: These exercises assume you have logged into your HPC cluster and are ready to enter login shell commands.

Question 1

Given the procedures described in this lesson, enter the appropriate commands into your  login shell to:

  1. Change your currently running shell to another available shell, e.g., tcsh or ksh.
  2. Confirm that your currently running shell has been changed.
  3. Confirm that your default shell has not been changed.


Question 2

You changed your default shell from bash to tcsh by using the exec command. You then enter the command "echo $SHELL" and see that the value is still set to bash. What went wrong?