What Are Bash Variables and How Can You Use Them?

How To Use Bash Variables Featured

Bash allows you to map whole strings of text to single variables, simplifying its use and script writing. How do they work, and how can you use them? Let’s find out.

What’s a Variable?

Variables are easy-to-remember names that can contain different alphanumeric values. They’re useful because they allow the same function to be applied on different values, without having to rewrite a script/piece of code. They also make writing the script/piece of code easy, since instead of dealing with individual values, you can use the same name for all of them.

Realtime Variables

Bash allows the use of variables. You can create variables on the fly and reuse them during your current Bash session. They can assist your use of Bash in many different ways, and they’ll be gone after the current session ends.

For example, let’s say you’re visiting a bunch of sites. You could be doing research or scraping data. You could create the following variable:

How To Use Bash Variables Realtime Variable

After that, if you wanted to visit our site with Firefox, you can just type:

How To Use Bash Variables Open Variable In Firefox

Much easier – and more readable. The $sitea variable would remain mapped to the site until you either changed its contents manually or the Bash session ended. And, of course, you can create more variables, such as siteb, sitec, and sited.

When setting up new variables, you can use any names you wish and store any alphanumeric strings inside them. Do keep in mind, though, that they’re case-sensitive by default. Thus, $sitea would not be the same as $SiteA. Also, note that you should use quotation marks when storing strings with special characters inside them (including spaces).

Variables in Scripts

Variables in Bash are more useful when writing scripts since they allow you to write a single script, which can then iterate through different strings or act on customized pieces of data. Let’s say that you’re writing a script that everyone could use on their computer, but each time would display a personalized greeting. Without variables, you’d have to write a different version of the script for each user. With variables, you keep the script the same and only change the user’s name.

Such a script would look something like the following:

How To Use Bash Variables A Simple Script

The above example may seem redundant; however, as the complexity of the code increases, variables become indispensable. A script could be hundreds or thousands of lines long and contain the user’s name in different spots. To better understand it, consider the following somewhat different script:

How To Use Bash Variables Repeated Variable Instances

The above script will use the name defined as the username variable to complete the text. If using the actual user’s name, you’d have to type it four times. Then, do the same for the next user, and another four times for the next. Again and again. By assigning it to a variable, you only have to change it once for each user, and every mention of the user’s name in the text will be updated.

Permanent Bash Variables and Aliases

We saw how you can temporarily set variables and how, for something more permanent, you can include them in your own scripts. Isn’t it possible, though, to permanently set variables in Bash? The answer’s a big “yup!” and you only have to edit a single file: “~/.bashrc”.

Open the file “~/.bashrc” in your favorite text editor. Since I prefer nano, I did it with:

How To Use Bash Variables Open Bashrc

We suggest you begin with a test run, only adding a single variable, so that you’ll know where to look if the process doesn’t work out. Move to the end of the file and, in a new line, add your variable. For example, I set a variable for my name as:

How To Use Bash Variables Bashrc With New Variable

Save your file and exit the editor. The tweaks won’t be applied immediately. Enter the following in your terminal for it to take effect:

Now you can make use of the newly set variable in your Bash session:

You can set up as many variables as you like and vastly simplify your daily adventures in Bash.

For an extra boost in productivity, it’s also worth setting up a different type of variable: aliases. Unlike typical variables, which are mapped to data you can use in commands, aliases are used instead of actual commands.

Just like you can use a simple-to-remember variable to hold long strings of text, you can use aliases as easy alternatives to complex commands. You can find more about them here, where we turn a whole 7zip compression command into a two-character alias.

As a final note, even if you’ve permanently set a variable in .bashrc, you can re-assign a different value to it temporarily, as we saw before. The variable will present the new content until the current Bash session ends (after logging out or restarting) or you re-source the .bashrc file.

Odysseas Kourafalos Odysseas Kourafalos

OK's real life started at around 10, when he got his first computer - a Commodore 128. Since then, he's been melting keycaps by typing 24/7, trying to spread The Word Of Tech to anyone interested enough to listen. Or, rather, read.

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.