3- BASH Scripting – Basic scripting & using variables in script

Hello Linux-fanatics, this is the part 3 of BASH Scripting series. In this tutorial, we will be finally writing our first BASH script & also use variables in our scripts.

As already discussed in Part 1 of the series, first line of a script is “#!/bin/bash” AKA Shebang, which basically tells that the scripts are to run in BASH.

First Script

# this script shows who is logged in, their present working directory & date,time

That’s it, just save the file and also provide executing permissions to the scripts,

chmod +x firstscript.sh

NOTE We use “#” to write comments in scripts, these comments will not show up in our script’s output.


Modified First Script

Above script was meant to just show how scripts looks like. We can modify to display some more messages

# this script tells who is logged in, present working directory & date,time
echo “Let’s see who is logged in”
echo “Current working directory is”
echo “Date & Time is”

Save & execute. Echo command will print the text in the quotes along with output of the commands.


Using Variables in scripts

In Part 2 tutorial of series, we learned about Environment Variables. Now we can use those in our scripts or we can also define some of our own variables for scripts.

Example We will be using some environment variables like HOME, UID & USER to show user’s information

# This will show us current user’s information
echo “ Current user is : $USER “
echo “ $USER has $UID UID”
echo “ & his home path is $HOME”

The script will show current user, their PID & home path.

This script only has environment variables but we can also define some user variables inside our scripts

# testing user variables
echo “$name will go to school in $days days”
echo “$name will go to school in $days days”


Command Substitution

In scripting while declaring a variable , we can also define output of a command as the variable’s input. This is known as command substitution,  & the way we do this is “ user=$(who) ”. You will find yourself using command substitution very much when we go deep into scripting.

Let’s see an example

# shows command substitution
echo “date & time are: ” $var1

This is pretty basic example of command substitution.

Here is another example, let’s assume you have to take backup of /home/test/logs file daily & with the below mentioned script

# copy the /home/test/logs directory listing to a log file
today=$(date +%y%m%d)
ls /home/test/logs -al > log.$today

Nice example of how scripting makes life easy.


So this concludes our Part3 of the BASH scripting. We created our first script, used some variables & command substitution in our scripts.

Will be back soon with next tutorial, meanwhile practice as much you can & let me know if you have any queries or suggestions.

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Donate us some of you hard earned money:

Linux TechLab is thankful for your continued support.


Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

4 Responses

  1. marcodellav says:

    Hi, thanks for the great tutorial(s).
    One question: on the last example script above, shouldn’t this line

    date=$(date +%y%m%d)


    today=$(date +%y%m%d)

    I am new to scripting, so forgive me if I’m wrong!

  2. tom says:

    i love to do bash scripting after reading this. thank you for posting

  3. vijay says:

    This blogs helps to understand basic unix script.

Leave a Reply

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