Customizing Your Computing Environment with Unix Commands

Site: HPC - Moodle
Course: Customizing Your Computing Environment
Book: Customizing Your Computing Environment with Unix Commands
Printed by: Guest user
Date: Saturday, May 3, 2025, 10:00 PM

1. Introduction

There are two main topics included in this lesson. They will give you a basic understanding of how to identify and modify the computing environment on your HPC resource to meet the needs of your software application. You should read all the lesson content and perform the exercises to get the most from the course.

The Login Shell

In this section, you will learn about a computer program called the login shell, used to interact with an HPC resource. At the end of the lesson, you will know what a login shell is and the various shell programs available. You will also be able to identify your default shell program and how to change it to another shell program.

Environment Variables

In this section, you will learn about variables the login shell uses that affect your user environment. At the end of the lesson, you will be about to list the environment variables and their values set for your user environment. You will also be able to change their values and create new ones.

2. The Login Shell

When you log into your HPC Cluster, a terminal window opens with a command-line interpreter or login shell running. The figure below shows a terminal window after logging into the Illinois Campus Cluster. A shell is a computer program that provides a user interface for typing commands to interact with the underlying operating system. In Unix or Unix-like systems, the shell is used to enter single text commands line-by-line or multiple commands via a file called a shell script. Essentially, the shell translates the commands you enter into a form the computer can understand.

terminal window running a login shell

Figure: A terminal window running a login shell program on the Illinois Campus Cluster.

Several shell programs are available, each providing similar functionality but different syntax and capabilities. Most fall within one of two classes: those descended from the Bourne shell (sh) and those from the C Shell (csh). The Bourne shell is the original shell program developed for the Unix operating system. The C shell was created as an alternative to the Bourne shell to provide a syntax similar to the C programming language. The table below gives examples of some of their commonly used descendant shells.

Shell Class Description

Bourne-again shell (bash)

sh

An open-source extension of the Bourne shell with added features. It is the default shell for Linux.

Korn shell (ksh)

sh

An extension of the Bourne shell with several features adapted from the C shell.

TC shell (tcsh)

csh

A revision of the C shell with substantially expanded capabilities.


An HPC cluster will have several shells installed, with one of them set as the default. In most cases, the default shell will be bash. In the next section, you will learn how to tell which shell you have.

2.1. Identifying Your Shell

Now that you know what a login shell is and about the various shell programs available, let's find out how to identify your default shell program.

To find your default shell program, enter the following command at the shell prompt:

echo $SHELL

The output from this command shows a directory path along with the filename of the default shell. In the example, you can see that the default shell is bash.

[username@dt-login02 ~]$ echo $SHELL
/bin/bash
[username@dt-login02 ~]$

Typically, you can find which other usable shells are installed on your HPC Cluster by viewing the /bin directory.

Icon indicating an exercise

Exercise

Note: This exercise assumes that you have logged into your HPC Cluster and are ready to enter commands into your login shell.

Enter the command into your login shell to identify which shell program is your default.

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?

3. Environment Variables

In Unix, there are variables, called environment variables, that store system values which affect your user environment. These values are available in every shell you initialize during a session and are used by the shell and programs you execute on the shell command line. Many UNIX commands and tools such as compilers, debuggers, profilers, and editors look in your environment variable's list to find the specific information they need. 

Previously, you used the command 'echo $SHELL' to find which shell was set as your default. By doing this, you asked the system to display the value of the environment variable named SHELL. The variable name is preceded with a dollar ($) sign to tell the shell you are entering a variable name rather than issuing a command. 

By convention, environment variable names are in uppercase and consist of only letters, numbers, or the underscore character (_).


3.1. Environment Variable Values

Many environment variables on an HPC cluster are preset for you by the system administrator. Examples include:

  • PATH - a list of directory paths to search for executable files when you type a command without providing the full path.
  • HOME - the location of your home directory.
  • SHELL - the full filename of your default shell.
  • HOSTNAME - the hostname assigned to the resource.

You can find the value of any of these variables using the Unix echo command. For example, to find the value of the PATH environment variable, you would enter the command 'echo $PATH.'

Below is a sample session using the echo command to display the values of the PATH and HOME environment variables.

output from echo $path command     

Figure: Using the echo command to display individual environment variable values.

You can display the values of all of your environment variables using the Unix env command with no command options. A sample session using the env command is shown below. The output has been truncated to save space.

Output from env command     

Figure: Using the env command to display all environment variable values.

The sample output above shows that the variables are listed as keyword/value pairs separated by an equal (=) sign. For example, SHELL=/bin/bash means that the environment variable's value SHELL is /bin/bash.

3.2. Modifying Environment Variables

You can customize your environment by adding new environment variables or changing existing ones. However, you may not be able to change some of the variables depending on the permissions set by your system administrator. This is usually the case for variables such as HOSTNAME, SCRATCHDIR, and so on.

To change or assign a new value to an environment variable, use the Unix setenv command for C-type shells (e.g., tcsh or csh) or the Unix export command for Bourne-type shells (e.g., bash or ksh). The syntax for these commands is as follows:

setenv <variable name> "value"
export <variable name>="value"

To create a new environment variable, you assign a value to a variable name that does not already exist. For example, if you store your project files in a directory named /work/, you could create an environment variable to store the directory name. Below is a sample session using the bash shell that assigns this pathname to an environment variable named MY_WORK.

Using export command to define a variable named MY_WORK     

Figure: Using the setenv command to create an environment variable and set its value.

In the sample session, we confirmed that the variable had been created correctly using the echo command to display its value. If you were using a C-type shell such as tcsh you would have entered the following command:

setenv MY_WORK="/work/"

3.3. Exercise

Icon indicating an exerciseExercise

  1. List all of the environment variables set for your user environment.
  2. Create a new environment variable called MYFILE and set it equal to myfile.txt.
  3. Display the value of the environment variable MYFILE.

4. Summary

In this lesson, you first learned about the login shell, some commonly used shell programs, and how to use the shell to interact with the underlying operating system on a UNIX, or UNIX-like, computer.

Next, you learned about environment variables, how they affect the user environment on your computing resource, and how to modify them to customize your user environment. As the complexity of your environment increases, setting your environment variables this way will become cumbersome. In the next lesson, you will learn a simpler way that uses the Environment Modules package.