How is a variable maintained in Linux?

Updated: 06/30/2020 by Computer Hope
variable maintained in linux

The command line shell of the Linux operating system is a powerful tool which is, at its core, a programming language interpreter. As with any other programming language, the command shell lets you use variables to store, retrieve, and manipulate data.

Setting and retrieving variable values

In the bash command shell, which is the default shell used in Linux, set a variable using the general syntax NAME=VALUE. For instance, at the command prompt, you can type:

x=5

And a variable named x will be set to the numeric value 5. Variable names can be in uppercase or lowercase, or a mixture of both, and they are case-sensitive. When setting the variable, there must be no spaces between the variable name, equals sign, and value.

There is no need to declare the variable beforehand, or specify its data type. Bash determines the type of the variable based on its value.

For instance, our variable can also contain a string:

x=Hello

For a simple string, it's not necessary to enclose the string in quotation marks, but if the string contains spaces, you must surround it with double-quotes:

x="Hello, World!"

To retrieve the value of your variable, prefix it with a dollar sign. For instance, to view the value of x using the echo command, you would run:

echo $x

Which outputs:

Hello, World!

What happens here is, the shell takes the value of your variable and inserts it into your command. You can refer to the variable anywhere in your command, even inside a double-quoted string:

echo "$x $x Can anybody hear me? I said, $x"
Hello, World! Hello, World! Can anybody hear me? I said, Hello, World!
Tip

Variables are only interpreted inside a string if you use double-quotes. If you use single-quotes, the variable name is interpreted literally, with no interpretation:

echo '$x $x Can anybody hear me? I said, $x'
$x $x Can anybody hear me? I said, $x

Setting the value of a variable to be the output of a command

To set the value of your variable to be the output of a specific command, use the form:

NAME=$(COMMAND)

For example, if your username is "steven," running the whoami command returns the value steven. You can set this to a variable:

myname=$(whoami)
echo $myname
steven

You can do this for any command. If the command takes arguments, you don't need to enclose the command in quotes. Everything inside the parentheses is run as a command:

x="Hello, World!"
x=$(echo $x$x$x)
echo $x
Hello, World!Hello, World!Hello, World!

Using variables in a simple script

Let's see how we can use a variable in a simple backup script. Let's say you want to back up everything in the directory documents in your home directory. The script archives everything in that directory, and any directories it contains, into a tar archive whose name contains the current date. Let's keep the backups in a directory called backups in your home directory (we'll assume this directory already exists. If not, you can run mkdir ~/backups to create it).

Open a text editor and place this text in a new file:

#!/bin/bash

MYFILENAME=~/backups/documents-backup-$(date +%Y-%m-%d).tar.gz

tar -czf $OUTPUTFILE ~/documents

Let's look at each line of this script. The first line, #!/bin/bash, declares the script as an executable file to run with /bin/bash, which is the executable of the bash command shell.

Note

The file is not executable until we chmod the file later, but this line is required in any proper bash script.

The second line sets the variable MYFILENAME, which contains the name of the archive containing our backup. The first part of the file name is our backups directory, ~/backups.

Note

The tilde character (~) is a special directory name that always represents our home directory.)

Next, we have the beginning of the file name, documents-backup-. Then, we insert the current date using the date command, the plus symbol, and the date format string %Y-%m-%d. For instance, if the date was February 23, 2015, that would return 2015-02-23.

Finally, we use the extension .tar.gz, which is the proper file name extension for a gzip-compressed tar file. So, if we ran this script on February 23, 2015, the value of MYFILENAME would be ~/backups/documents-backup-2015-02-23.tar.gz. If we ran it on May 7, 2015, the value would be ~/backups/documents-backup-2015-05-07.tar.gz.

The third line is the command that creates the archive. We use the tar command with the option -czf, which creates (c) a gzipped archive (z) using the specified file name (f). Then we specify the archive file name by referring to the variable we created in line 2, OUTPUTFILE. Lastly, we tell tar what it is that we want to archive: the directory ~/documents.

To run the script, save the text file in your home directory (or anywhere else you prefer) and give it an appropriate name, for instance, backup-my-docs. There is no need to give it a file name extension. Exit the text editor, and make the file executable using the chmod +x command, for instance:

chmod +x ~/backup-my-docs

Now you can run the script. For instance, if the script is in your working directory, you can run it with:

./backup-my-docs

Or, if it's in your home directory, you can run it from any directory at all with the command:

~/backup-my-docs