Shell variables are important for the smooth running of any Linux system. For example, every time you run a program or command, your system looks in the PATH variable to check if the program is present. Knowledge of shell variables is important for your daily use or administration of Linux systems.

The Importance of Shell Variables

A shell variable is a placeholder for storing information used by your Linux system, programs, or scripts.

There are different types of shells or command-line interpreters on Linux, all of which use variables extensively. Some of the important roles that shell variables play include:

  • Shell variables allow you to store and manipulate values in the shell, and can manipulate results in calculations or other operations.
  • They provide a way to pass values between scripts and programs
  • You can use shell variables to customize the behavior of programs

Shell variables are very flexible; you can create your own custom variables too, but here are some of the default variables that come with your system.

1. PATH

The PATH variable specifies the directories where the system should search for executable files when you enter a command in the terminal.

It is a system-wide environment variable and comprises a colon-separated list of directories. Each directory is searched in turn for an executable file with the same name as the command you entered.

For example, if you enter the ls command in the terminal, the system will search through the directories specified in the PATH variable for the command named ls. If it finds the file, it will run it; if not, it will display the “command not found” error message.

The PATH variable is important because it allows you to run commands from any directory without having to specify the full path to the executable file. For example, if you have the "ls" executable file in the /usr/bin directory, you can run the ls command from any directory by simply typing ls, rather than specifying the full path, i.e. /usr/bin/ls.

You can view the current value of the PATH variable by running the echo command as follows:

        echo $PATH
    
the path environment variable on a linux system

You can also modify the value of the PATH variable by adding or removing directories from the list. This can be useful if you want to add a directory containing your own executables or custom scripts, or if you want to modify the directories that are searched when running commands.

2. HOME

On Linux, the HOME variable is an environment variable that specifies the path to the user's home directory. Why is this so important? Well, Linux is a multi-user OS and the home folder is a special directory on your system associated with each user.

The home directory is typically used to store the user's personal files, such as documents, pictures, music, etc.

The HOME variable is typically set automatically when you log in and is used by many programs and scripts to locate the user's home directory. For example, when you run the cd command without any arguments, it uses the HOME variable to navigate to the user's home directory.

Many programs use the HOME variable to store configuration files or other data in the user's home directory. You can view the current value of the HOME variable by running the echo $HOME command.

3. USER

The USER variable specifies the username of the currently logged-in user. It is set automatically when you log in and is commonly used by programs and scripts to determine the current user's username.

The USER variable is often used in scripts to customize the behavior of a program based on the current user. For example, a script might check the value of the USER variable and only perform certain actions if the current user has root or sudo privileges.

You can also use the variable for running certain generic commands. For example, when adding your user to the docker group, you can simply run the following command instead of typing the actual username:

        sudo usermod -aG docker $USER
    

4. SHELL

A shell is a program that provides a command-line interface (CLI) for interacting with the operating system.

Most Linux systems use the Bourne Again Shell (Bash) as the default, but there are plenty of alternatives. The C shell (csh), Z shell (Zsh), fish, etc.

The SHELL variable specifies the path to the user's default shell. It is set automatically when you log in.

Further, the variable is used in scripts to customize the behavior of programs based on the user's default shell. For example, a script might check the value of the SHELL variable and use different syntax or options depending on the shell being used.

You can view the current value of the SHELL variable by running the echo $SHELL. You can change your default shell using the chsh command.

5. PWD

Yet another important environment variable on Linux, PWD stores your current working directory. Unlike most environment variables that are mostly static, this one is updated by the shell whenever you change directories using the cd command.

You can use the PWD variable in your shell scripts to easily refer to the current working directory. The value of the variable is similar to the one you get when you run the pwd command.

6. HOSTNAME

hostname on a linux system

A hostname or device name is a unique name used to identify your computer on a network. For example, when you want to SSH into a machine, you can simply use the hostname instead of the IP address.

The HOSTNAME variable is an environment variable that simply specifies the hostname of the system. It is set automatically when the system boots and is typically used to identify the system when it is connected to a network. There are several ways in which you can change the hostname on Linux.

The HOSTNAME variable is also used by programs and scripts to identify the system and customize its behavior based on the hostname. A script will usually check the value of the HOSTNAME variable and only perform certain actions if the system is a specific host.

Creating a Custom Variable on Linux

Apart from the default Linux variables that are automatically set for you. You can also define your own shell variables by using the export command. To create a new variable called GREETING with the value "Hello World", you could use the following command:

        export GREETING="Hello World"
    

You can then use the GREETING variable in your scripts or within the command line just like any other environment variable.