YYisak
Published on

Variables in Linux shell environment

Authors

Introduction

Variables are placeholders for another value. They are used to store information that can be referenced and manipulated in a shell script or command line. In Linux, variables are used to store data, such as strings, numbers, file names, and more. They can be used to make scripts more dynamic and reusable.

In Linux shell environment, there are different types of variables:

  • Local Variables: These variables are only available within the scope of the script or shell session in which they are defined.

    • Available to the current shell process only.
    • They are not inherited by sub processes.
    • Can be used to store temporary data or configuration settings.
    • To define a local variable, you can use the following syntax:
      zsh
      $ VARIABLE_NAME=value
      
  • Environment Variables: These variables are available to all processes that run in the shell environment. They can be accessed by any script or program that runs in the shell.

    • Are available both in a specific shell session and in sub processes spawned from that shell session.
    • Can be used to pass configuration data to commands which are run.
    • The majority of the environment variables are in capital letters (e.g. PATH, DATE, USER).
    • A set of default environment variables provide, for example, information about the user’s home directory or terminal type.
    • Sometimes the complete set of all environment variables is referred to as the environment.

Working with Global Variables

To make a variable available to subprocesses, turn it from a local into an environment variable. This can be done by using the export command.

zsh
$ export VARIABLE_NAME=value

Or

zsh
$ VARIABLE_NAME=value
$ export VARIABLE_NAME

CAUTION

Make sure to not use $ when running export as you want to pass the name of the variable instead of its contents.

To check whether it is available to sub processes, you can run the following command:

zsh
$ bash -c 'echo $VARIABLE_NAME'

To remove an environment variable, you can use the unset command:

zsh
$ unset VARIABLE_NAME

To change the value of an environment variable, you can simply assign a new value to it:

zsh
$ VARIABLE_NAME=new_value

NOTE

There is no space on either side of equal sign

After changing the value of an environment variable export it again to make it available to sub processes. You can view all environment variables with env (not alphabetized) and set (alphabetized)

Some environment variables

VariableDescription
LOGNAMEThe name of the user who is currently logged in.
OLDPWDThe previous working directory.
OSTYPEThe type of operating system.
PATHA list of directories that the shell searches for commands.
USER and USERNAMEThe name of the user who is currently logged in.
HOST and HOSTNAMEThe name of the computer.
ENVThe name of the shell.
EUIDThe effective user ID of the current user.
HISTFILEThe name of the file that stores command history.
HISTSIZEThe number of commands to remember in the command history.

The PATH environment variable

The PATH environment variable is a colon-separated list of directories that the shell searches for commands. When you type a command in the shell, the shell looks for the command in each directory in the PATH variable in order. If the command is found in one of the directories, the shell executes it.

To view the value of the PATH environment variable, you can use the echo command:

zsh
$ echo $PATH

To add a directory to the PATH environment variable, you can use the following command:

zsh
$ export PATH=$PATH:/new/directory

Conclusion

Variables are part of shell scripting and command line usage in Linux. They allow you to store and manipulate data, pass configuration settings to commands, and make your scripts more dynamic and reusable. Understanding the difference between local and environment variables and how to define, export, and unset variables can help in working effectively in the Linux shell environment.