How to Use Variables in Linux?

Learn via video courses
Topics Covered

Variables in shell scripting, especially in Bash, have different characteristics and behaviors when compared to low-level programming languages. In shell scripts, variables act as containers for holding strings, and they do not possess memory addresses. Unlike formal programming languages, shell scripts do not require explicit variable type declarations. Instead of configuring the system, variables in shell scripts are mostly used for referring and altering data within the script.

Examples

Here are some examples of Linux variables:

  • Variable Names:
    Variable names in Linux shell scripting must follow specific rules. They must begin with a letter (A-Z or a-z) or an underscore (_). They can include letters, numerals, and underscores, but they cannot begin with a digit or contain spaces or special characters. Unix shell variables are named in UPPERCASE by convention.

    Here are a few examples of valid Linux variable names:

    Here are some examples of invalid Linux variable names:

    The reason we avoid using characters like !, *, or - in variable names is that the shell interprets these characters with special meanings.

  • Defining Variables:
    We use the equals symbol (=) to declare a variable in Linux. The syntax is as follows:

    Here are some examples of how to define a variable:

    In the above example, we define two variables: name and age. The variable name is assigned the value "Rakesh," representing a person's name, while the age is set to 23, representing a person's age. These Linux variables are scalar because they can only hold one value at a time.

  • Accessing Values:
    To access the stored value of a variable, use the dollar symbol ($) followed by the variable name. Consider the following script, which reads the value stored in the variable "NAME" and displays it on the STDOUT:

    The output of the above script will be as follows:

  • Read-only Variables:
    You can make a variable read-only using the readonly command. Once a variable is marked as read-only, its value cannot be modified. For example, the following script generates an error when attempting to modify the value of NAME:

    The output of the above script will be as follows:

  • Unsetting or deleting a variable:
    When you unset or delete a Linux variable, the shell eliminates it from the list of variables it tracks. When you unset a Linux variable, you can no longer retrieve the variable's stored value. A Linux variable that has been defined as read-only using the readonly command cannot be deleted or unset with the unset command. The syntax for unsetting a declared Linux variable with the unset command is as follows:

    Here's a simple example that shows how the unset command works:

    The above example generates no output.

  • Types of Variables in Linux Shell:
    There are various types of shell variables in the Linux shell:

    1. Local variables:
      Local variables are limited to the current shell or script. They are only created and accessible within the function or script in which they are defined. Local variables in Linux are no longer accessible once the function or script execution ends.
    2. Environment variables:
      Environment variables have a global scope and are accessible to all processes that are started from the current shell session. Certain programs rely on particular environmental conditions to function properly. In most cases, a shell script defines just the environment variables required by the programs it executes, ensuring that the programs have access to the necessary settings for their proper functioning.
    3. Shell variables:
      A shell variable is a specific variable that is set by the shell and is essential for the shell to work properly. Some of these variables are environment variables, while others are local variables.

Environment Variables

Environment variables have a global scope and are accessible to all processes that are started from the current shell session. Some programs require environment variables to perform properly. Typically, a shell script defines just the environment variables required by the programs it runs.

List of Environment Variables

Several environment variables in Linux are either set by the system by default or that users and programs can define for various purposes. Some of the more common environmental variables are listed below.

Environmental VariablesDescription
PATHA colon-separated list of directories that will be searched when commands are executed.
HOMERepresents the path to the home directory of the current user.
USERStores the username of the currently logged-in user.
LOGNAMERepresents the name of the current user.
PWDThe current working directory.
SHELLSpecifies the path of the current user’s shell.
LANGDefines the current locale settings.
MAILThe location where user mail is stored.
TERMSpecifies current terminal emulation.

Commands to List All the Environment Variables on Your Machine

To get a list of all the environment variables on the machine, use the env or printenv commands. Both commands will provide a list of all environment variables that are presently configured in your system. Here is how to use each command:

Using the "env" command:

using-env-command-to-list-all-the-enviornment-variable

Using the "printenv" command:

using-printenv-command-to-list-all-the-enviornment-variable

User-Defined Variables

Variables that are created and defined by the user within a program or script are known as user-defined variables. These variables are used to store, read, access, and manipulate data to meet the requirements of the user.

  • Variables Creation:
    User-defined variables in Linux can be created by simply assigning a value to them when working in the terminal or writing a script. Syntax:

    Example:

  • Referring to Variables:
    The value stored in any variable can be referred to by simply adding the '$' symbol before the variable name. Syntax:

    Example:

  • Reading a Value from a User during runtime:
    The read command can be used to read a value from the user while a shell script is running. With the read command, you can prompt the user for input and then store the provided value in a variable. Here's an example of how it works:

    When you run this script, the message "Enter your name:" will appear, and the user can input their name. After pressing Enter, the script will save the value entered into the variable name and then display the user's name.

  • Local Variables:
    Local variables are limited to the current shell or script. They are only created and accessible within the function or script in which they are defined. Local variables in Linux are no longer accessible once the function or script execution ends.

  • Global Variables:
    A global variable is a variable with a global scope, which means it can be accessed from anywhere in the program or script. Global variables, as compared to local variables, can be used and modified throughout the script. Global variables are normally defined at the top level of the script, outside of any functions or blocks.

Conclusion

  • Variables are an essential component of programming languages, serving as containers for storing and manipulating data in memory, either temporarily or persistently.
  • Variable names in the Linux shell must begin with a letter (A-Z or a-z) or an underscore (_).
  • User-defined variables can be created by simply assigning a value to them when working in the terminal or writing a script.
  • We can access the value stored in any variable by simply adding the ‘$’ symbol before the variable name.
  • We can use the read command to read a value from the user while a shell script is running.
  • Local variables are the variables that are only available in the current shell or script.
  • Global variables are the variables that can be accessed from anywhere in the program or script.
  • To get a list of all the environment variables on the machine, use the env or printenv commands.