Test your knowledge!Take a quiz to access yourself.

Shell Scripting – Know the basics

It might not be wrong to say that the Shell scripting is an art and Linux is the canvas or platform to create and show that art. But like any art or skill it has its own nuances, and these are sometimes called as rules. Using these rules you can create a logical and beautiful script which serves the purpose for which it has been written. In our last post, Shell Script – Start scripting now we explained how to start writing a simple script. We recommend reading that before continuing and if you are not so new to Scripting but want brush up your skills you can continue reading.

Shell locating a script file:

We know how to create a shell script and run it, but what if you want to run it from some other directory. In another words, you moved to some other directory and need to run a script which was in some other directory. Than you might answer that we can give either scripts’ absolute path of relative path. But have  you noticed that the programs which we install on our machine runs directly after installation by just typing a keyword. Have you ever thought how? And what if we want to run our shell script directly from any folder by just giving its name without giving any relative or absolute path?

The answer is yes we can. The programs which run directly using a keyword are installed in /bin and we save this location to the PATH, which is an environmental variable. So when we type the command keyword, shell looks into internal commands which are part of itself, if command is found execute it. Else look into all the directories mentioned in PATH settings. Again if found execute the command, if not look into the current directory, if found execute the command otherwise give error ‘bash:xxxx:command not found‘ . We can run our script in the same manner by using its name, we just have to keep our shell script under bin directory inside user’s home directory.

For example if user is root:

cd ~
mkdir bin
cd bin
pwd
/root/bin
touch test.sh

So if we give test.sh from any location it will execute the script. Isn’t a nice trick? But that doesn’t count in the basics of  shell scripting. Basics will start now 🙂 .

Variables and its rules is Shell Scripting:

Shell Script provides us with two types of  variables.

  1. System Variables: They are the one which are provided by the system and we can use them directly in our scripts. They are generally used to get the state of the system and are in capital letters. You can see system variables by entering command set in your console. Few important are:
    BASH, HOME, USERNAME, PWD, PATH, OSTYPE.
    To see there value just type:

    echo $BASH

    So the value will be printed like:

    /bin/bash

    So you can use them in your script like this and get the values.

  2. User Defined Variable:
    The user defined variable are those variables which are defined by the user to save custom values as per the requirement. To save values into the variable the basic syntax is variable_name=variable_value. But it has few rules which needs to adhered while assigning the values to the variable.
    Rules are as follow:

    1. Variable names starts with underscore(_) or English letter, and followed by alphanumeric characters or underscores(_).
    2. Special characters (?,*,#,$.. etc) are not allowed.
    3. There shouldn’t be space between variable and equal to(=) sign and value and equal to (=) sign.
    4. Variables are case sensitive.
    5. To define NULL value, just provide empty value or empty string.
      var1=10;               //Valid
      var2 = 10;             //Invalid
      var3 =10;              //Invalid
      Name="Talentcookie"    //Valid
      Name1=talentCookie     //Valid
      name1=TalentCookie     //Valid and the variable name1 is different than Name1.
      val1=                  //Valid and null Value
      val2=""                //Valid and null Value
      ?val="Invalid"         //Invalid

      Hope that clears the concept of variables and assigning variables while shell scripting.

Arithmetic in Shell Scripting:

We know how to create a variable and assign value to it. Let’s now try to use those variables to do basic arithmetic operations. To do basic arithmetic we use keyword expr which calculates the result of the operation. The basic syntax is expr opr1 operator opr2 . Following are the examples which will make concept clear:

expr 4 + 5  //9
expr 7 - 6  //1
expr 8 / 2  //4
expr 9 % 2  //1

To calculate the expression and print it we can combine same syntax with echo.

echo `expr 4 + 5` //9

The important point to remember is that we have used backquotes(`) (above tilde(~) key). If you use double quotes(“) or single quotes(‘). It will print the expr expression instead of calculating it.

echo "expr 4 + 5"  // expr 4 + 5

Also remember to keep space between Operator and operand(values), otherwise you will get just values printed like 4+5 instead of 9. That’s same with variables too.

a=10
b=15
echo `expr $a + $b`   //25
echo `expr $a+$b`     //10+15
echo "expr $a + $b"   //expr 10 + 15, though it prints everything inside back quotes (instead of adding values), still it remembers to print the variable value.

That’s all for this post, if you have any questions shoot them immediately in the comments and we will reply you asap. Also keep on Visiting as we have many upcoming posts on Shell Scripting. 🙂

Add a Comment

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