What are Bash variables and how do I use them?

What are Bash variables and how do I use them?

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.

Real-time variables

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:

How to use Bash Variables in real time

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

How to use Bash Open Variable in FirefoxHow to use Bash Open Variable in Firefox

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 siteb, sitec, and sited.

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:

How to use bash variables a simple scriptHow 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 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:

How to Use Bash Variables Repeated Variable InstancesHow to Use Bash Variables Repeated Variable Instances

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:

How to use Bash Open Bashrc variablesHow to use Bash Open Bashrc variables

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:

How to use Bash variables Bashrc with a new variableHow to use Bash variables Bashrc with a new variable

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.

Odysseas Kourafalos
Odysseas Kourafalos

OK’s real life began around 10 a.m. when he got his first computer – a Commodore 128. Since then he’s been melting keys typing 24/7, trying to get The Word Of Tech out to everyone who is. interested enough to listen. Or rather read.

Is this article useful?

!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version=’2.0′;
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,’script’,
‘https://connect.facebook.net/en_US/fbevents.js’);
fbq(‘init’, ‘400239050508508’);
fbq(‘track’, ‘PageView’);

bashbashrcLinuxvariables
0

More Similar Posts

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu