Working With Bash Variables In Linux

Working With Bash Variables In Linux

The Bourne Again SHell, or bash as it is abbreviated to, is probably the most used shell in the Linux world and is default on most Linux distributions. As such, it is what many people think of when they consider the Linux command line. Many people use bash for its flexibility as well as a way to run other applications. It is also a scripting language with functions and variables, allowing other applications to be pieced together to make more complicated wholes. Here, we’ll be looking at using bash variables and how they can help you.

What is a bash variable?

First, we’ll address what a variable is. In its most basic terms, a variable is a reference to a piece of the computer’s memory where it will store some data. When the variable is used in a command, the computer will fetch the data from memory and use it in the variable’s place. While this might sound complicated, it might be easier to think of a variable as similar to a named box. You put the data that you want to use in the box, and every time you use the name of the box, the name is replaced with its content.

Using bash variables

So how do you work with variables in bash? They are used by making an assignment with the equals symbol. For example:

var=”Hello World”

This puts the text “Hello World” into the variable named “var”. Then when the variable “var” is referenced later in the text, “Hello World” is used in its place. This variable is valid for your current session and when you close it and log back in the variable will have been forgotten.

A variable doesn’t need to contain random text. It can also store command information or the output from other commands that may be needed. For example:

mylogs=$(ls /var/log/*.log)

The command above uses the dollar symbol in conjunction with parentheses “$()” to indicate that bash should take the output from the ls command,  a list of current log files, and assign them to the mylogs variable, which can be referenced later. You can also use backticks “” around the command and you may see this used in bash scripts, but the parenthesis method is the modern recommendation.

To use a variable at the command line, you need to prefix its name with the dollar symbol $ to let bash know that you mean the contents of that variable:

echo $var

The command above would write “Hello World” to the screen as bash would replace $var with the “Hello World” that we assigned earlier. Another example could be:

zip $mylogs currentlogs.zip

This would create a zip file containing your current logs, which we previously put in the mylogs variable.

Environment variables

In addition to variables that you set for your shell session, there are variables referred to as environment variables. These variables are also available to applications started from your shell and normally are pre-configured using configuration files such as the user’s .bashrc or .bash_profile files. By convention, these are named using all capital letters.

To see a list of the current environment variables you can use the following command:


Some common environment variables are HOME which shows where the current user’s home directory is, and PATH which defines paths for the shell to look in to run applications. You can add environment variables by placing the export command before your variable assignment:

export MY_VAR=”My environment variable”

You can also export a previously declared variable to make it an environment variable:

export mylogs

To convert a variable back from being an environment variable the export command can be used with the -n flag:

export -n mylogs

Finally, if you are finished with a variable completely you can delete it with the unset command:

unset mylogs

Bash variables make working at the command line much easier as a simple way to store and recall data that you may want to use with multiple commands, saving you the hassle of repeatedly typing commands.

This article was brought to you by VPS.net, for dedicated server hosting, cloud servers and 24/7 support visit our site here vps.net

No Comments

over 200,000 servers launched

and counting worldwide...