How To Write Linux Shell Scripts (Part 2)

- - No comments
The last time we got a brief introduction to shell scripting and showed you some of its uses. We also learned how to write our first shell script and execute it. In this article, we’ll be moving forward with variables and other important topics. 

Introduction to Variables

What are variables? Variables are places in memory that are used by the computer to store specific data. Whoa! That sounds a bit confusing doesn’t it? Well, let me tell you that it’s not. Remember in algebra you used to assign a certain value, let’s say 3 to x and then another value, say 6 to y? And then, you used to write x = 3 and y = 6. Once that was done, you used to perform various operations using x and y. For example, x+y = z, x*y, and x2 + y2 = z2. Wasn’t that simple?

In other words, you gave the value of 3 to x and of 6 to y, so that you could perform operations on them without having to repeat them. This makes sure that you don’t end up writing 3+6 and 3*6 every time. Variables in programming and shell scripting work the same way. They store a particular value given to them and let the user use that variable multiple times. To get a clearer picture, let’s go back to our first script: 

## Script that greets the user, prints the date 
echo “Hello World!”
echo "Welcome to shell scripting, $USER"
echo -n "Today is  ";date

In that example, we had used the variable ‘$USER’. The $USER is a system-defined variable that stores the current user’s username. This variable is created in Linux by default. (Hence the name System-defined variable). So, whenever you write echo $USER in the terminal, it will print out your username. 

Now that we know what variables are, it’s time to see them in action. Open a new file and type in the following code: 

echo "Let's learn about variables"
echo "My favorite Operating System is $myos and my favorite site is $mysite "

Once you’re done, save the file as and type in the following commands:

$chmod +x

(Note that chmod +x command sets the permission for the file to executable)

The output will be something like this:

Let's learn about variables
My favorite Operating System is Linux and my favorite site is TechSource 

Code Explained:

Now, in the third line, we have assigned the value TechSource to the variable mysite. And similarly Linux to myos. So every time you invoke $myos with echo, it will print out its value rather than the variable name. Hence, when we type $myos and $mysite in the last statement, it prints out their values thus giving the output: My favorite Operating System is Linux and my favorite site is TechSource. 

You can similarly define as many variables as you want. In fact, you can try out variables in the terminal itself. Go ahead, open the terminal and type in the following commands:

$echo $n 

It will print out the value of n, which is 32.

Variables are very useful in shell scripting as they can be used to temporarily or permanently store specific data like pathname and file size. 

Points to Remember: 

1. While assigning values to variables, make sure that there are no spaces in the declaration: 

n=10    is correct
n = 10  is wrong

2. Variables are case sensitive.


is NOT the same as


3. Variable names must start either with a number or an alphabet. You can also begin a variable name with an underscore, (example: n=_32) but that doesn’t look good. That said, you could add an underscore in between a variable name (example:  HOME_NAME=matrix).


1. Write a shell script that prints out the following line: “Do or do not. There is no try” on one line, and on the next, it prints “Luke, I am your father”.  Store the name of Luke in a variable. 

2. Store your age and weight in two different variables (call them age and weight) and print the following line using their values: "Hi, my age is 22 and my weight is 80 kg".

That’s it for now. See you in the next part of the series. Happy scripting!

Written by: Abhishek, a regular TechSource contributor and a long-time FOSS advocate.

No comments

Post a Comment