The 6 Useful Bash Shell Variables You Should Know About

Shell variables are important to the proper functioning of any Linux system. For example, every time you run a program or command, your system looks in the PATH variable to see if the program is present. Knowledge of shell variables is important for everyday 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 in Linux, all of which use variables extensively. Some of the important functions that shell variables perform 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 also create your own custom variables, but these are some of the default variables that come with your system.

1. ROUTE

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

It is a system-wide environment variable and consists of 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 command ls in the terminal, the system will search 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 show the error message “command not found”.

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 executable file “ls” in the /usr/bin directory, you can run the ls command from any directory by simply typing lsinstead of specifying the full path, i.e. /usr/bin/ls.

You can see 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 that contains your own executables or custom scripts, or if you want to modify the directories that are searched when running commands.

2. HOUSE

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 operating system and the home folder is a special directory on your system associated with each user.

The home directory is generally used to store the user’s personal files such as documents, images, music, etc.

The HOME variable is usually 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 see the current value of the HOME variable by running the echo $ HOME domain.

3. USER

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

The USER variable is often used in scripts to customize a program’s behavior based on the current user. For example, a script can 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 to execute 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 many alternatives. The C layer (csh), the Z layer (Zsh), the fish, etc.

The SHELL variable specifies the path to the user’s default shell. It is automatically configured at login.

Additionally, 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 see the current value of the SHELL variable by running the echo $SHELL. You can change your default shell using the chsh command.

5. PCD

Another important environment variable in Linux, PWD stores your current working directory. Unlike most environment variables which are mostly static, the shell updates this each time you change directories with the cd command.

You can use the PWD variable in your shell scripts to easily query the current working directory. The variable’s value is similar to what you get when you run the pwd command.

6. HOST NAME

hostname on a linux system

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

The HOSTNAME variable is an environment variable that simply specifies the host name 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 to change the hostname in Linux.

Programs and scripts also use the HOSTNAME variable to identify the system and customize its behavior based on the host name. A script will generally check the value of the HOSTNAME variable and only perform certain actions if the system is a specific host.

Creating a custom variable in Linux

As well as the default Linux variables that are automatically set for you. You can also define your own shell variables 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 like any other environment variable.

Leave a Reply

Your email address will not be published. Required fields are marked *