Bash Scripting Tutorial #2: Declaring Variables

When writing scripts, it is often important to use and declare variables. Declaring variables is super easy. There are multiple ways to declare them, but they each work the same way. One way that I often employ is to prompt users for their input, place said input in a variable, and then use the variable to tell the script what the user wants. By this method, I am using the variable as a wrapper for something else. Variables are great for projects in coding where you really don’t know what the output of the variable will be, but you know what you want the variable to do. Other commands can be used as value for variables and declaring a word or a number as a variable and giving it value allows the echo command to print that value to the screen when typing echo $variable. A good example of declaring and calling a variable would be opening a terminal and typing var1=$(cat filename | grep “RandomPattern”) then typing echo $var1. Typing echo with the variable name will result in the value of that variable being displayed. Filename is just an example as we didn’t actually call an actual file with anything inside it. Grep would have looked for the specified pattern in quotes and that would have been the value. Another example would be:

num1=1

echo $num1

My output in this scenario would be the number one. Variables are often the first thing you learn in programming classes as these are used throughout whatever project you are trying to accomplish. While scripting languages are different from Java and or C, the idea is roughly the same. Across environments, these variables are declared and used in roughly the same ways. Python to Bash to Ruby, they all use them. A final example will be an excerpt from one of my own personal scripts.

echo “Enter the name of any software you’d like to install

read software

This example relies on the user to give input and then uses the read command to register that input as the value of the variable known as software. When the command continues it would run similar to this:

sudo pacman -S –noconfirm $software

Where the above command would install all software specified and stored in $software. Variables can encompass values that are classified as Integers, Strings and booleans. Booleans can be either True or False for the value. Unlike other variable types, these variables are best suited for a case where the outcome of a scenario is uncertain. The previous examples were of integer and string value respectively, but now it is time to see a True/False boolean variable in action. An example of a boolean variable would be this:

while True; do

some command

break

done

Another form of this would be:

find /etc/hosts.bak

while [ $? -gt 0 ]

do

sudo cp /etc/hosts /etc/hosts.bak

break

done

This form looks for the file /etc/hosts.bak. If the file is not found, in this case, if the scenario is false it will continue to create the file and then break out of the loop. If the value were True or “0”, It would have simply returned the file name in question. We will get further into this and While loops at a later time.

Leave a Reply


Vivaldi