Variables in the Linux Shell [Basic Guide]

Even if you’re not a programmer, you may have heard of variables. They are a way for the computer to store values, be they numbers or a set of letters, so that they can be used later for some processing.

The Linux shell also allows the user to create variables to store values, numbers, names, dates, and even the result of commands.

Various configurations of the system itself are stored in variables, which can be changed at any time, allowing the customization of the system.

Linux Shell Prompt PS1 variable

The user’s first contact with the Linux shell is the command prompt.

How this command prompt appears may change with each distribution, from the simplest, with a simple “$” or with user information, server name, and even the current directory:

[laugh @server1 ~] $

The command prompt can be customized by configuring a variable that the Linux shell maintains called PS1.

The variables are loaded at the start of the bash execution and can be set manually at any time.

The first variable that we are going to address is PS1 or simply Prompt String 1.

This variable stores the content of the bash command prompt when it is ready to receive commands.

There is also the PS2 variable that stores the content of the command prompt when multiple lines are needed to complete a command.

These two shell variables do not affect how the interpreter will process incoming commands, but they can be of great help when loading extra information such as user name, current directory, etc.

It was agreed that the variables are all written in an upper-case format. But it’s important to know that $name and $NAME are two different variables for the shell.

How to see the content of a variable?

The content of any Shell variable can be viewed with the echo command followed by the $ symbol plus the variable name:

$echo $PS
 $

The character says that any other character that succeeds it must be interpreted exactly as it is and not processed by the shell. It is what we call a metacharacter.

It is common for the standard bash command prompt to come in the form: u@ h: W $

Commands and variables in Linux are case-sensitive. Each of these characters is interpreted in a special way.

The u is used to represent the user’s name, the h is used to represent the system name (hostname) and the W is the current directory.

An example for this prompt is: uira @notebook:home$

$PATH variable

Another important Shell variable is PATH. The path stores a list of the directories containing the programs that you can run without having to pass the full path to your location on the command line.

$echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin

It’s important to know that the bash command interpreter follows the following order to find and execute the entered commands:

  1. Is the command entered an internal command of the command interpreter?
  2. If not, is the command an executable program located in a directory listed in the PATH variable?
  3. Was the location of the command explicitly stated?

An interesting tip is to include “:. ” at the end of the PATH variable for bash to include the current directory in its search list for executable programs.

Unlike MS-DOS, the current directory is not included in the standard search list. Without including the “:.” in the PATH it is necessary to enter the relative path when calling programs in the current directory with”. /program name”.

This is a problem for new Linux users who don’t understand because typing a command or trying to run a program that is in the current folder doesn’t work most of the time. It’s simply the lack of “:.” in $PATH.

Set command to view or change variables in Linux

A complete list of shell variables can be obtained with the command:

$set

You can also change or create a new shell variable using the command:

$ BOOK=” Linux Certification”

Preferably, variables should be declared in an upper box and do not place spaces between the variable name, the = symbol and its content.

If the content is alphanumeric, it is desirable that it be surrounded by single or double quotes.

Difference between single and double quotes in Linux

When the text of a variable is a common sequence of letters and numbers, it doesn’t matter if you’re going to use single or double quotes.

$ PHRASE1=” This is a test”
$ frase2='Of the difference between quotation marks'
$ echo $PHRASE1 $PHRASE2
This is a quotation mark difference test

But if you are going to use variables in quotes, there is a difference:

$echo “$PHRASE1 $PHRASE2”
This is a quotation mark difference test

$ echo '$PHRASE1 $PHRASE2' $PHRASE1 $PHRASE2

So double quotes expand the content of variables, while single quotes don’t. This makes a difference if you want, for example, to include a directory in the PATH variable:

$echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin

$ PATH="$PATH: /oracle/admin/bin”
$echo $PATH /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin:/oracle/admin/bin

In this case single quotes wouldn’t work.

Export a variable in the shell

After creating a shell variable, you must export it to the environment with the export command.

When a variable is exported to the environment, it is available to all shell child processes (all programs and applications that you run in bash).

Each time a program is executed by the shell, it will only receive variables created by the shell if they are exported with the export command.

So the child process (the program that we want to execute) will inherit the variables created from the parent process (the shell).

See the example below, where we created a variable called BOOK:

$ BOOK=” Linux Certification”
$ echo $BOOK
Linux Certification

If we run bash again in the same terminal to create a child process, you will see that the BOOK variable does not exist because it was not exported to the child processes:

$bash
$ echo $BOOK

Use of Jokers

When we are working with files and directories in the shell, it is very common to have to work with several files at once. To make this type of task simpler, Linux offers the wild card feature for file and directory names.

As with the deck of cards, jokers are symbols that can mean many things. On Linux, they allow you to use them as part of the name of files and directories. See the table below:

It

SymbolDescription
*means “anything goes” and can replace one or more characters in a name. For example, “Certifi*” can mean “Certificate”, “Certification”, or any other combination of file names starting with “Certifi”.
?It means that you can replace a character only in a name. For example: “? Certificate” can replace “Certificate”, “certificate”, or any other combination of file names starting with any character and ending with “certificate”.
{text1, text2…}Replace the part inside the braces with text1, then text2, and so on. For example: “part_ {a, b, c}” will result in “part_a”, “part_b” and “part_c”.

Examples:

$ ls *.txt

Lists all files with the suffix .txt

$ cat doc??? >> documents

Concatenate all files starting with “doc” that are 6 characters long in the documents file.

There are still special variables in Linux, used to create shell scripts.

Conclusion

If you need to store any value or data in order to be used later, variables are essential. And you need to know the main variables of the Linux shell, such as PS1 and PATH.

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Leave a Reply 0

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


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Need help?