Welcome to the spoken tutorial on Basics of Shell Scripting.
In this tutorial, we will learn about
* System variables
* User defined variables and
* Accepting user input via keyboard
To follow this tutorial you should be familiar with Linux Operating System
If not, for relevant tutorials please visit our website which is as shown
For this tutorial I am using
* Ubuntu Linux 12.04 Operating System and
* GNU Bash version 4.1.10
Please NoteGNU Bash version 4 or above is recommended for practice.
Let us start with a introduction to variables
* Bash variables provide temporary storage for information
* These variables can be used within the lifespan of the program.
* There are two types of variables System variables User defined variables
System variables, These are created and maintained by Linux Bash Shell itself.
They are defined by Capital letters.
Commonly used system variables are
* BASH_VERSION,
* HOSTNAME,
* HOME etc
Let us open the terminal by pressing Ctrl Alt and T keys simultaneously on your keyboard.
Now type set and press Enter.
This will display all the system variables.
Alternately, you can type env or printenv, to view all the system variables.
Let me clear the prompt
Now, type echo space within double quotes dollar sign HOSTNAME
and Now press Enter.
The hostname of the system will be displayed.
Now let's find out the full path of homedirectory.
Type echo space within double quotes dollar sign HOME(in Capital)
Press Enter.
The full path of user's home directory will be displayed.
Now, type
echo space within double quotes HOME (in capital)
Press Enter.
This will display only the HOME not the value of HOME variable.
So it is neccessary to use dollar sign( '$') at the beginning of every variable, to display its value.
Let us switch back to our slides
User Defined Variables
* These variables are created and maintained by users.
* It is always a good idea to avoid uppercase for the names of user defined variables.
* This makes it easy to differentiate between user defined and system variables.
Switch back to our terminal
Type username equal to sign sunita
Please note that there should not be any blank space between username, equal to sign and sunita.
Now press Enter.
To display the value of variable username
Type echo space within double quotes dollar sign username
press Enter
This will display sunita on your terminal.
The value of a variable can be unset,
Let us switch back to our slide
unset, the value of variable can be unset by using the unset command
The syntax for this is unset variablename
Let's use the previous example where username is our variable.
Switch to the Terminal. Now type unset space username press Enter
Let us check Type echo space within double quotes dollar sign username press Enter.
Nothing will be displayed on the terminal.
This means that the value of variable username has been removed.
Now switch back to our slide
Global and local variables.
* In Shell script, user defined variables can be declared globally or locally.
* By default, all variables are global.
* This means, their values remains the same inside and outside the function.
Let us learn how to declare variables globally and locally.
Switch to the terminal and type
gedit space g_(underscore)variable.sh space & (ampersand sign)
gedit is the text editor g_(underscore) variable.sh is our file name
and & (ampersand) is use to free up the prompt.
press Enter.
Type the code as shown here, in your g_(underscore)variable.sh file.
Let me explain the code now.
The first line, with the hash and exclamation symbol, is a shebang or a bang line.
username=sunita is the userdefined variable and it is declared globally.
echo will display the string outside function: And
dollar username will print the value of the variable username.
This is how we defined a function in BASH script.
We will discuss about functions in detail, in later tutorials.
This is the body of the function.
Here another message inside function will be displayed, along with the value of username.
Here, we call the function
This is our code. Now let's execute it.
Come back to our Terminal.
Let me clear the prompt
First we need to make our file executable.
Type chmod space plus x space g_(underscore)variable.sh press Enter
Now type dot slash g_(Underscore)variable.sh
Press Enter
Observe the output.
Outside the function, username takes the value sunita.
Inside the function also, username takes the same value sunita.
This is because username was declared globally outside the function.
Next, let us learn how to declare a variable locally.
Type gedit space l_(Underscore)variable.sh space & (ampersand sign)
Press Enter.
Type the code as shown here, in your l_(underscore)variable.sh file.
Let me explain the code.
The code is the same as before, except for an extra line of code inside the function.
Inside the function block, we have a line,local space username equals to jack
This assigns a new value for the variable username locally.
Now switch to the Terminal.
Let's make file executable
By Typing chmod space plus x space l_variable.sh
Press Enter.
Type dot slash l_variable.sh
Press Enter.
The output is displayed
Outside the function, username takes the value sunita.
Whereas inside the function, username takes the value jack.
This is because username is assigned this value locally, within the function.
Now let us quickly see how to get user input via keyboard.
The read command is used to accept input from the keyboard.
It can also be used to assign an input value to a user defined variable.
The syntax of read command is
read space hyphen p space within double quotes PROMPT
Please note that PROMPT is just a string, that waits for user input.
You may replace it with your own string.
Now Switch to the terminal
Typegedit space read.sh space & (ampersand sign)
Press Enter.
Type the code as shown, in your read.sh file.
Let me explain the code.
In this example, input is given from the keyboard by the user.
This is the bang line.
Here -p displays the prompt, without a newline and takes input from the keyboard.
The user input will be stored in the variable username.
echo command displays the message
Hello and the name entered by the user, via the keyboard.
So, let us execute the programme.
Come back to our terminal
Type chmod space plus x space read.sh
press Enter.
Typedot slash read.sh press Enter.
Here it is displayed Enter username:
I will type ashwini press Enter.
The message Hello ashwini is displayed.
ashwini was assigned as an input value to the user defined variable username.
Let us go back to our slide and summarise.
In this tutorial we learnt,
* System variables
* User defined variables
* Accepting user input via keyboard
As an assignment
Write a simple Bash program to get the following system variables
* pwd and * logname
Write a simple Bash program
* To ask username from user
* To exit the program, if user does not enter anything, within 10 seconds
* {Hint: read -(Hyphen)t 10 -(Hyphen)p}
Watch the video available at the link shown below.
It summarises the Spoken Tutorial project
If you do not have good bandwidth, you can download and watch it
The Spoken Tutorial Project Team Conducts workshops using spoken tutorials Gives certificates to those who pass an online test
For more details, please write to contact@spoken-tutorial.org
Spoken Tutorial Project is a part of the Talk to a Teacher project
It is supported by the National Mission on Education through ICT, MHRD, Government of India
More information on this Mission is available at the link shown below http://spoken-tutorial.org MEICT-Intro
The script has been contributed by FOSSEE and Spoken Tutorial Teams.
This is Ashwini Patil from IIT Bombay.Signing off.Thank you for joining.