Bash allows you to map entire strings of text to single variables, making it easier to use and write scripts. How do they work and how can you use them? Let’s find out.
What is a variable?
Variables are easy-to-remember names that can contain different alphanumeric values. They are 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 it easier to write the script / piece of code, because instead of dealing with individual values you can use the same name for all of them.
Bash allows the use of variables. You can create variables on the fly and reuse them during your current Bash session. They can help you use Bash in a number of ways, and they will disappear after the current session ends.
For example, let’s say you visit a bunch of sites. You could do research or extract data. You can create the following variable:
After that, if you want to visit our site with Firefox, you can just type:
Much easier – and more readable. the
$sitea The variable would remain mapped to the site until you manually edit its content or the Bash session ended. And, of course, you can create more variables, such as
When setting up new variables, you can use any names you want and store all alphanumeric strings inside. Keep in mind, however, that they are case sensitive by default. So,
$sitea would not be the same as
$SiteA. Also note that you must use double quotes when storing strings containing special characters (including spaces).
Variables in scripts
Variables in Bash are more useful when writing scripts because they allow you to write a single script, which can then iterate through different strings or act on custom data items. Let’s say you write a script that anyone could use on their computer, but display a personalized greeting each time. Without variables, you will have to write a different version of the script for each user. With variables, you keep the same script and only change the name of the user.
Such a script would look like this:
The above example may seem redundant; however, as the complexity of the code increases, variables become indispensable. A script can be hundreds or thousands of lines long and contain the user’s name in different places. To better understand this, consider the following slightly different script:
The above script will use the name defined as
username variable to complete the text. If you are using the actual user’s name, you will need to type it four times. Then do the same for the next user and four more times for the next. Again and again. By assigning it to a variable, you will 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’ve seen how you can temporarily set variables and how, for something more permanent, you can include them in your own scripts. Isn’t it possible, however, to permanently define variables in Bash? The answer is a big “yeah!” and you only have to edit one file: “~ / .bashrc”.
Open the “~ / .bashrc” file in your favorite text editor. Since I prefer the nano, I did it with:
We suggest that you start with a test, adding only a single variable, so that you know where to look if the process isn’t working. Go to the end of the file and, in a new line, add your variable. For example, I defined a variable for my name like:
Save your file and exit the editor. The changes will not be applied immediately. Enter the following into your terminal for it to take effect:
You can now use the newly defined variable in your Bash session:
You can configure as many variables as you want and greatly simplify your daily adventures in Bash.
For a further increase in productivity, it is also worth setting up another type of variable: aliases. Unlike typical variables, which are mapped to data that you can use in commands, aliases are used instead of actual commands.
Just as 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 read more about them here, where we turn an entire 7zip compress command into a two character alias.
As a final note, even if you have permanently defined a variable in .bashrc, you can temporarily reassign it to a different value, as we saw earlier. The variable will present the new content until the end of the current Bash session (after logging out or restarting) or until you resuscitate the .bashrc file.
Is this article useful?