0% found this document useful (0 votes)
14 views44 pages

Introduction to Shell Scripting Basics

The document provides an overview of shell scripting, explaining the concept of a shell as a command interpreter for Linux systems, primarily focusing on the Bash shell. It details how to write and execute shell scripts, the use of variables, and various operators, including arithmetic and relational operators. Additionally, it covers commenting methods and the importance of permissions when executing scripts.

Uploaded by

arunkumarhema
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views44 pages

Introduction to Shell Scripting Basics

The document provides an overview of shell scripting, explaining the concept of a shell as a command interpreter for Linux systems, primarily focusing on the Bash shell. It details how to write and execute shell scripts, the use of variables, and various operators, including arithmetic and relational operators. Additionally, it covers commenting methods and the importance of permissions when executing scripts.

Uploaded by

arunkumarhema
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

1

Shell Scripting

Shell is a software program that allows you to interact and access a computer system. User can
enter commands in the shell prompt, which will be executed by the shell. Since the only means
of communication through shell is text, it is known as Command-Line-Interface or CLI.

A shell is a command interpreter and serves as a user interface to the Linux kernel.

Prompts A prompt is a character or string of characters (such as $ or #) that the shell displays
when it is ready to receive a new command.
Under Linux, there are some powerful tools that for all practical purposes are unavailable under
Windows .
One of these tools is something called "shell programming". This means writing code that a
command shell executes.

The most common Linux shell is named "Bash". The name comes from "Bourne Again SHell," .

There are two primary ways to use the shell: interactively and by writing shell scripts.
 In the interactive mode, the user types a single command (or a short string of commands)
and the result is printed out.
 In shell scripting, the user types anything from a few lines to an entire program into a text
editor, then executes the resulting text file as a shell script.
A shell script is a plain-text file that contains shell commands. It can be executed by typing its
name into a shell, or by placing its name in another shell script.

A shell script file may optionally have an identifying suffix, like ".sh".
Execute a shell script this way:
$ ./[Link]
This special entry is a way to tell the command processor that the desired script is located in the
current directory.
2

What Kernel Is?

Kernel is heart of Linux Os. It manages resource of Linux Os. Resources means facilities
available in Linux. For e.g. Facility to store data, print data on printer, memory, file management
etc .
Kernel decides who will use this resource, for how long and when. It runs your programs (or set
up to execute binary files). The kernel acts as an intermediary between the computer hardware
and various programs/application/shell.

What is Linux Shell ?


Computer understand the language of 0's and 1's called binary language.

In early days of computing, instruction are provided using binary language, which is difficult for
all of us, to read and write. So in Os there is special program called Shell. Shell accepts your
instruction or commands in English (mostly) and if its a valid command, it is passed to kernel.
Shell is a user program or it's a environment provided for user interaction. Shell is an command
language interpreter that executes commands read from the standard input device (keyboard) or
from a file.
Shell is not part of system kernel, but uses the system kernel to execute programs, create files
etc.

Several shell available with Linux including:


3

What is Shell Script?

Normally shells are interactive. It means shell accept command from you (via keyboard) and
execute them. But if you use command one by one (sequence of 'n' number of commands) , the
you can store this sequence of command to text file and tell the shell to execute this text file
instead of entering the commands. This is known as shell script.

Shell script defined as:

"Shell Script is series of command written in plain text file. Shell script is just like batch file is
MS-DOS but have more power than the MS-DOS batch file."

How to write shell script?


Following steps are required to write shell script:

(1) Use any editor like vi or mcedit to write shell script.


(2) After writing shell script set execute permission for your script as follows.

syntax:
chmod permission your-script-name

Examples:
$ chmod +x your-script-name
$ chmod 755 your-script-name

Note: This will set read write execute(7) permission for owner, for group and other
permission is read and execute only(5).
(3) Execute your script as
4

Sh [Link]
Or
./sh [Link]

NOTE: Be sure to place a linefeed at the end of your script before executing. Forgetting a
terminating linefeed is a common beginner's error.

Writing and execution of shell script

Example 1.
1. Create a script by typing the following two lines into a file using your favourite editor(vi
editor).

$ cat [Link]
#!/bin/bash
echo Hello World
2. You can choose any name for the file. File name should not be same as any of the Linux
built-in commands.
3. Script always starts with the two character ‘#!’ which is called as she-bang. This is to
indicate that the file is a script, and should be executed using the interpreter (/bin/bash)
specified by the rest of the first line in the file.

4. Execute the script as shown below.

$ bash [Link]

Hello World
5. When you execute the command “bash [Link]”, it starts the non-interactive shell
and passes the filename as an argument to it.
6. The first line tells the operating system which shell to spawn to execute the script.

7. In the above example, bash interpreter which interprets the script and executes the
commands one by one from top to bottom.
5

8. You can even execute the script, with out leading “bash” by:

o Change the permission on the script to allow you(User) to execute it, using the command
“chmod u+x [Link]”.

o ./sh [Link]

o Directory containing the script should be included in the PATH environment variable. If
not included, you can execute the script by specifying the absolute path of the script.

9. echo is a command which simply outputs the argument we give to it. It is also used to print the
value of the variable.

vi editor setting password for a file and encrypting the file


in order to encrypt a file or save the file with password use the -x option.

vi -x [Link]

This will prompt for an password, Enter the password.

whoever reads the file without the password will not be able to see the contents of the file.

In order to edit the file use the same command.

vi -x [Link]

Variables
Variables are areas of memory that can be used to store information and are referred to by a
name.
Whenever the shell sees a word that begins with a "$", it tries to find out what was assigned to
the variable and substitutes it.

How to Create a Variable


6

To create a variable, put a line in your script that contains the name of the variable followed
immediately by an equal sign ("="). No spaces are allowed. After the equal sign, assign the
information you wish to store.
Where Do Variable Names Come From?
you get to choose the names for your variables. There are a few rules.
1. Names must start with a letter.
2. A name must not contain embedded spaces. Use underscores instead.
3. You cannot use punctuation marks.

Defining Variables:

Variables are defined as follows::

variable_name=variable_value

For example:

NAME="Zara Ali"

Accessing Values:

To access the value stored in a variable, prefix its name with the dollar sign ( $):

For example, following script would access the value of defined variable NAME and would print
it on screen

NAME="Zara Ali"
echo $NAME

This would produce following value:

Zara Ali

Read-only Variables:

The shell provides a way to mark variables as read-only by using the readonly command. After a
variable is marked read-only, its value cannot be changed.

For example, following script would give error while trying to change the value of NAME:
7

NAME="Zara Ali"
readonly NAME
NAME="Qadiri"

This would produce following result:

/bin/sh: NAME: This variable is read only.

Variable Types:

When a shell is running, three main types of variables are present:

 Local Variables: A local variable is a variable that is present within the current instance
of the shell. It is not available to programs that are started by the shell. They are set at
command prompt.
 Environment Variables: An environment variable is a variable that is available to any
child process of the shell. Some programs need environment variables in order to
function correctly. Usually a shell script defines only those environment variables that are
needed by the programs that it runs.
 Shell Variables: A shell variable is a special variable that is set by the shell and is
required by the shell in order to function correctly. Some of these variables are
environment variables whereas others are local variables.

The read Statement


Use to get input (data from user) from keyboard and store (data) to variable.

Syntax:
read variable1, variable2,...variableN

Following script first ask user, name and then waits to enter name from the user via keyboard.
Then user enters name from keyboard (after giving name you have to press ENTER key) and
entered name through keyboard is stored (assigned) to variable fname.

echo "Your first name please:"


read fname
echo "Hello $fname, Lets be friend!"

Your first name please: vivek


Hello vivek, Lets be friend!
8

Comments
One line of shell code can be "commented out" using the "#" character. Sometimes
however it would be nice to "comment out" more than one line of code, like the C "/* */"
comments.

Multi line comment: Method 1


To comment multiple lines code you need to add <<COMMENT and COMMENT tags.

<<COMMENT

What ever written here is a comment

COMMENT

The statements in between the comment line will not be executed.

Example
echo "Hii guys"
#starting of multi line comment
<<COMMENT
read a
echo "No line in this block will execute"
COMMENT
echo "Hello Guys"
exit

Multi line comment: Method 2

Another way to comment out multiple lines is this:

The character ":" (colon) followed by a ' (single quote) is used for multi line comment in a shell
script. It should terminate with a single quote. There should be a space between colon character
and single quote.

Syntax:

: 'first line comment


second line in of comment
still comment.'
9

Example

echo "Hii guys"


#starting of multi line comment
: '
read a
echo "No line in this block will execute"
'
echo "Hello Guys"
exit

Operators
There are various operators supported by each shell. Our tutorial is based on default shell
(Bourne) so we are going to cover all the important Bourne Shell operators in the tutorial.

There are following operators which we are going to discuss:

 Arithmetic Operators.
 Relational Operators.
 Boolean Operators.
 String Operators.
 File Test Operators.

The Bourne shell didn't originally have any mechanism to perform simple arithmetic but it uses
external programs, either awk or the must simpler program expr.

Here is simple example to add two numbers:

val=`expr 2 + 2`
echo "Total value : $val"

This would produce following result:

Total value : 4

There are following points to note down:

 There must be spaces between operators and expressions for example 2+2 is not correct,
where as it should be written as 2 + 2.
 Complete expression should be enclosed between ``, called inverted commas.
10

Arithmetic Operators:

There are following arithmetic operators supported by Bourne Shell.

Assume variable a holds 10 and variable b holds 20 then:

Operator Description Example

Addition - Adds values on either side of the


+ `expr $a + $b` will give 30
operator

Subtraction - Subtracts right hand operand from


- `expr $a - $b` will give -10
left hand operand

Multiplication - Multiplies values on either side of


* `expr $a \* $b` will give 200
the operator

Division - Divides left hand operand by right hand


/ `expr $b / $a` will give 2
operand

Modulus - Divides left hand operand by right hand


% `expr $b % $a` will give 0
operand and returns remainder

= Assignment - Assign right operand in left operand a=$b would assign value of b into a

Equality - Compares two numbers, if both are same


== [ $a == $b ] would return false.
then returns true.

Not Equality - Compares two numbers, if both are


!= [ $a != $b ] would return true.
different then returns true.

It is very important to note here that all the conditional expressions would be put inside square
braces with one spaces around them, for example [ $a == $b ] is correct where as [$a==$b] is
incorrect.

All the arithmetical calculations are done using long integers.

Here is an example which uses all the arithmetic operators:

#!/bin/sh

a=10
b=20
val=`expr $a + $b`
11

echo "a + b : $val"

val=`expr $a - $b`
echo "a - b : $val"

val=`expr $a \* $b`
echo "a * b : $val"

val=`expr $b / $a`
echo "b / a : $val"

val=`expr $b % $a`
echo "b % a : $val"

if [ $a == $b ]
then
echo "a is equal to b"
fi

if [ $a != $b ]
then
echo "a is not equal to b"
fi

This would produce following result:

a + b : 30
a - b : -10
a * b : 200
b/a:2
b%a:0
a is not equal to b

There are following points to note down:

 There must be spaces between operators and expressions for example 2+2 is not correct,
where as it should be written as 2 + 2.
 Complete expression should be enclosed between ``, called inverted commas.
 You should use \ on the * symbol for multiplication.

Relational Operators:

Bourne Shell supports following relational operators which are specific to numeric values. These
operators would not work for string values unless their value is numeric.
12

For example, following operators would work to check a relation between 10 and 20 as well as in
between "10" and "20" but not in between "ten" and "twenty".

Assume variable a holds 10 and variable b holds 20 then:

Operator Description Example

Checks if the value of two operands are equal or


-eq [ $a -eq $b ] is not true.
not, if yes then condition becomes true.

Checks if the value of two operands are equal or


-ne not, if values are not equal then condition becomes [ $a -ne $b ] is true.
true.

Checks if the value of left operand is greater than


-gt the value of right operand, if yes then condition [ $a -gt $b ] is not true.
becomes true.

Checks if the value of left operand is less than the


-lt value of right operand, if yes then condition [ $a -lt $b ] is true.
becomes true.

Checks if the value of left operand is greater than


-ge or equal to the value of right operand, if yes then [ $a -ge $b ] is not true.
condition becomes true.

Checks if the value of left operand is less than or


-le equal to the value of right operand, if yes then [ $a -le $b ] is true.
condition becomes true.

It is very important to note here that all the conditional expressions would be put inside square
braces with one spaces around them, for example [ $a <= $b ] is correct where as [$a <= $b] is
incorrect.

Here is an example which uses all the relational operators:

#!/bin/sh

a=10
b=20

if [ $a -eq $b ]
then
echo "$a -eq $b : a is equal to b"
else
echo "$a -eq $b: a is not equal to b"
13

fi

if [ $a -ne $b ]
then
echo "$a -ne $b: a is not equal to b"
else
echo "$a -ne $b : a is equal to b"
fi

if [ $a -gt $b ]
then
echo "$a -gt $b: a is greater than b"
else
echo "$a -gt $b: a is not greater than b"
fi

if [ $a -lt $b ]
then
echo "$a -lt $b: a is less than b"
else
echo "$a -lt $b: a is not less than b"
fi

if [ $a -ge $b ]
then
echo "$a -ge $b: a is greater or equal to b"
else
echo "$a -ge $b: a is not greater or equal to b"
fi

if [ $a -le $b ]
then
echo "$a -le $b: a is less or equal to b"
else
echo "$a -le $b: a is not less or equal to b"
fi
This would produce following result:
10 -eq 20: a is not equal to b
10 -ne 20: a is not equal to b
10 -gt 20: a is not greater than b
10 -lt 20: a is less than b
10 -ge 20: a is not greater or equal to b
10 -le 20: a is less or equal to b

There are following points to note down:


14

 There must be spaces between operators and expressions for example 2+2 is not correct,
where as it should be written as 2 + 2.

Boolean Operators:

There are following boolean operators supported by Bourne Shell.

Assume variable a holds 10 and variable b holds 20 then:

Operator Description Example

This is logical negation. This inverts a true


! [ ! false ] is true.
condition into false and vice versa.

This is logical OR. If one of the operands is true


-o [ $a -lt 20 -o $b -gt 100 ] is true.
then condition would be true.

This is logical AND. If both the operands are true


-a then condition would be true otherwise it would be [ $a -lt 20 -a $b -gt 100 ] is false.
false.

Here is an example which uses all the boolean operators:

#!/bin/sh

a=10
b=20

if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi

if [ $a -lt 100 -a $b -gt 15 ]


then
echo "$a -lt 100 -a $b -gt 15 : returns true"
else
echo "$a -lt 100 -a $b -gt 15 : returns false"
fi

if [ $a -lt 100 -o $b -gt 100 ]


then
15

echo "$a -lt 100 -o $b -gt 100 : returns true"


else
echo "$a -lt 100 -o $b -gt 100 : returns false"
fi

if [ $a -lt 5 -o $b -gt 100 ]


then
echo "$a -lt 100 -o $b -gt 100 : returns true"
else
echo "$a -lt 100 -o $b -gt 100 : returns false"
fi
This would produce following result:
10 != 20 : a is not equal to b
10 -lt 100 -a 20 -gt 15 : returns true
10 -lt 100 -o 20 -gt 100 : returns true
10 -lt 5 -o 20 -gt 100 : returns false

There are following points to note down:

 There must be spaces between operators and expressions for example 2+2 is not correct,
where as it should be written as 2 + 2.

String Operators:

There are following string operators supported by Bourne Shell.

Assume variable a holds "abc" and variable b holds "efg" then:

Operator Description Example

Checks if the value of two operands are equal or


= [ $a = $b ] is not true.
not, if yes then condition becomes true.

Checks if the value of two operands are equal or


!= not, if values are not equal then condition becomes [ $a != $b ] is true.
true.

Checks if the given string operand size is zero. If it


-z [ -z $a ] is not true.
is zero length then it returns true.

Checks if the given string operand size is non-zero.


-n [ -z $a ] is not false.
If it is non-zero length then it returns true.

str Check if str is not the empty string. If it is empty [ $a ] is not false.
16

then it returns false.

Here is an example which uses all the string operators:

#!/bin/sh

a="abc"
b="efg"

if [ $a = $b ]
then
echo "$a = $b : a is equal to b"
else
echo "$a = $b: a is not equal to b"
fi

if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi

if [ -z $a ]
then
echo "-z $a : string length is zero"
else
echo "-z $a : string length is not zero"
fi

if [ -n $a ]
then
echo "-n $a : string length is not zero"
else
echo "-n $a : string length is zero"
fi

if [ $a ]
then
echo "$a : string is not empty"
else
echo "$a : string is empty"
fi
This would produce following result:
abc = efg: a is not equal to b
abc != efg : a is not equal to b
17

-z abc : string length is not zero


-n abc : string length is not zero
abc : string is not empty

File Test Operators:

There are following operators to test various properties associated with a Unix file.

Assume a variable file holds an existing file name "test" whose size is 100 bytes and has read,
write and execute permission on:

Operator Description Example

Checks if file is a block special file if yes then


-b file [ -b $file ] is false.
condition becomes true.

Checks if file is a character special file if yes then


-c file [ -b $file ] is false.
condition becomes true.

Check if file is a directory if yes then condition


-d file [ -d $file ] is not true.
becomes true.

Check if file is an ordinary file as opposed to a


-f file directory or special file if yes then condition [ -f $file ] is true.
becomes true.

Checks if file has its set group ID (SGID) bit set if


-g file [ -g $file ] is false.
yes then condition becomes true.

Checks if file has its sticky bit set if yes then


-k file [ -k $file ] is false.
condition becomes true.

Checks if file is a named pipe if yes then condition


-p file [ -p $file ] is false.
becomes true.

Checks if file descriptor is open and associated


-t file [ -t $file ] is false.
with a terminal if yes then condition becomes true.

Checks if file has its set user id (SUID) bit set if


-u file [ -u $file ] is false.
yes then condition becomes true.

Checks if file is readable if yes then condition


-r file [ -r $file ] is true.
becomes true.

Check if file is writable if yes then condition


-w file [ -w $file ] is true.
becomes true.
18

Check if file is execute if yes then condition


-x file [ -x $file ] is true.
becomes true.

Check if file has size greater than 0 if yes then


-s file [ -s $file ] is true.
condition becomes true.

Check if file exists. Is true even if file is a


-e file [ -e $file ] is true.
directory but exists.

Here is an example which uses all the file test operators:

Assume a variable file holds an existing file name "/var/www/tutorialspoint/unix/[Link]" whose


size is 100 bytes and has read, write and execute permission on:

#!/bin/sh

file="/var/www/tutorialspoint/unix/[Link]"

if [ -r $file ]
then
echo "File has read access"
else
echo "File does not have read access"
fi

if [ -w $file ]
then
echo "File has write permission"
else
echo "File does not have write permission"
fi

if [ -x $file ]
then
echo "File has execute permission"
else
echo "File does not have execute permission"
fi

if [ -f $file ]
then
echo "File is an ordinary file"
else
echo "This is sepcial file"
fi
19

if [ -d $file ]
then
echo "File is a directory"
else
echo "This is not a directory"
fi

if [ -s $file ]
then
echo "File size is zero"
else
echo "File size is not zero"
fi

if [ -e $file ]
then
echo "File exists"
else
echo "File does not exist"
fi
This would produce following result:
File has read access
File has write permission
File has execute permission
File is an ordinary file
This is not a directory
File size is zero
File exists

Shell Decision Making


Unix Shell supports conditional statements which are used to perform different actions based on
different conditions. Here we will explain following two decision making statements:

 The if...else statements


 The case...esac statement

The if...else statements:

If else statements are useful decision making statements which can be used to select an option
from a given set of options.

Unix Shell supports following forms of if..else statement:

 if...fi statement
20

 if...else...fi statement

 if...elif...else...fi statement

Most of the if statements check relations using relational operators discussed in previous chapter.

The case...esac Statement:

You can use multiple if...elif statements to perform a multiway branch. However, this is not
always the best solution, especially when all of the branches depend on the value of a single
variable.

Unix Shell supports case...esac statement which handles exactly this situation, and it does so
more efficiently than repeated if...elif statements.

There is only one form of case...esac statement which is detailed here:

 case...esac statement

Unix Shell's case...esac is very similar to switch...case statement we have in other programming
languages like C or C++ and PERL etc.

If…fi statement

The if...fi statement is the fundamental control statement that allows Shell to make decisions and
execute statements conditionally.

Syntax:

if [ expression ]
then
Statement(s) to be executed if expression is true
fi

Here Shell expression is evaluated. If the resulting value is true, given statement(s) are executed.
If expression is false then no statement would be not executed. Most of the times you will use
comparison operators while making decisions.

Example:
a=10
b=20
21

if [ $a == $b ]
then
echo "a is equal to b"
fi

if [ $a != $b ]
then
echo "a is not equal to b"
fi

This will produce following result:

a is not equal to b

if...else...fi statement

The if...else...fi statement is the next form of control statement that allows Shell to execute
statements in more controlled way and making decision between two choices.

Syntax:

if [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi

Here Shell expression is evaluated. If the resulting value is true, given statement(s) are executed.
If expression is false then no statement would be not executed.

example:

a=10
b=20

if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
22

if...elif...fi statement

The if...elif...fi statement is the one level advance form of control statement that allows Shell to
make correct decision out of several conditions.

Syntax:

if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
Statement(s) to be executed if expression 3 is true
else
Statement(s) to be executed if no expression is true
fi

There is nothing special about this code. It is just a series of if statements, where each if is part of
the else clause of the previous statement. Here statement(s) are executed based on the true
condition, if non of the condition is true then else block is executed.

Example : Compare Numbers


The below script reads two integer numbers from user, and checks if both the numbers are equal
or greater or lesser than each other.

echo "Please enter first number"


read first
echo "Please enter second number"
read second

if [ $first -eq 0 ] && [ $second -eq 0 ]


then
echo "Num1 and Num2 are zero"
elif [ $first -eq $second ]
then
echo "Both Values are equal"
23

elif [ $first -gt $second ]


then
echo "$first is greater than $second"
else
echo "$first is lesser than $second"
fi

case...esac

You can use multiple if...elif statements to perform a multiway branch. However, this is not
always the best solution, especially when all of the branches depend on the value of a single
variable.

Shell support case...esac statement which handles exactly this situation, and it does so more
efficiently than repeated if...elif statements.

Syntax:

The basic syntax of the case...esac statement is to give an expression to evaluate and several
different statements to execute based on the value of the expression.

case expression in
pattern1 )
statements ;;
pattern2 )
statements ;;
...
esac
Following are the key points of bash case statements:
 Case statement first expands the expression and tries to match it against each pattern.
 When a match is found all of the associated statements until the double semicolon (;;) are
executed.
 After the first match, case terminates with the exit status of the last command that was
executed.
 If there is no match, exit status of case is zero.
24

The interpreter checks each case against the value of the expression until a match is found. If
nothing matches, a default condition will be used.

There is no maximum number of patterns, but the minimum is one.

When statement(s) part executes, the command ;; indicates that program flow should jump to the
end of the entire case statement. This is similar to break in the C programming language.

Example:

#!/bin/sh

FRUIT="kiwi"

case "$FRUIT" in
"apple") echo "Apple pie is quite tasty."
;;
"banana") echo "I like banana nut bread."
;;
"kiwi") echo "New Zealand is famous for kiwi."
;;
esac

This will produce following result:

New Zealand is famous for kiwi.

Example :
inp1=12
inp2=11
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo -n "Please choose a word [1,2 or 3]? "
read oper
if [ $oper -eq 1 ]
then
echo "Addition Result " $(($inp1 + $inp2))
25

else
if [ $oper -eq 2 ]
then
echo "Subtraction Result " $(($inp1 - $inp2))
else
if [ $oper -eq 3 ]
then
echo "Multiplication Result " $(($inp1 * $inp2))
else
echo "Invalid input"
fi
fi
fi

$ ./[Link]
1. Addition
2. Subtraction
3. Multiplication
Please choose a word [1,2 or 3]? 4
Invalid input

Shell Loop Types


Loops are a powerful programming tool that enable you to execute a set of commands
repeatedly. In this tutorial, you would examine the following types of loops available to shell
programmers:

 The while loop


 The for loop

 The until loop

You would use different loops based on dfferent situation. For example while loop would
execute given commands until given condition remains true where as until loop would execute
until a given condition becomes true.

While Loop
26

The while loop enables you to execute a set of commands repeatedly until some condition
occurs. It is usually used when you need to manipulate the value of a variable repeatedly.

Syntax:

while command
do
Statement(s) to be executed if command is true
done

Here Shell command is evaluated. If the resulting value is true, given statement(s) are executed.
If command is false then no statement would be not executed and program would jump to the
next line after done statement.

Example:

Here is a simple example that uses the while loop to display the numbers zero to nine:

#!/bin/sh

a=0

while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done

This will produce following result:

0
1
2
3
4
5
6
7
8
9

Each time this loop executes, the variable a is checked to see whether it has a value that is less
than 10. If the value of a is less than 10, this test condition has an exit status of 0. In this case, the
current value of a is displayed and then a is incremented by 1.
27

For Loop
There are two types of bash for loops available. One using the “in” keyword with list of values,
another using the C programming like syntax.

Method 1: For Loop using “in” and list of values

Syntax:

for varname in list


do
command1
command2
..
done

In the above syntax:

 for, in, do and done are keywords


 “list” contains list of values. The list can be a variable that contains several words
separated by spaces. If list is missing in the for statement, then it takes the positional
parameter that were passed into the shell.
 varname is any Bash variable name.

Static values for the list after “in” keyword

In the following example, the list of values (Mon, Tue, Wed, Thu and Fri) are directly given after
the keyword “in” in the bash for loop.

$ cat [Link]
i=1
for day in Mon Tue Wed Thu Fri
do
echo "Weekday $((i++)) : $day"
done

$ ./[Link]
Weekday 1 : Mon
Weekday 2 : Tue
Weekday 3 : Wed
Weekday 4 : Thu
Weekday 5 : Fri
28

Caution: The list of values should not be separated by comma (Mon, Tue, Wed, Thu, Fri). The
comma will be treated as part of the value. i.e Instead of “Mon”, it will use “Mon,” as value as
shown in the example below.

$ cat [Link]
i=1
for day in Mon, Tue, Wed, Thu, Fri
do
echo "Weekday $((i++)) : $day"
done

$ ./[Link]
Weekday 1 : Mon,
Weekday 2 : Tue,
Weekday 3 : Wed,
Weekday 4 : Thu,
Weekday 5 : Fri

Caution: The list of values should not be enclosed in a double quote. (“Mon Tue Wed Thu Fri”).
If you enclose in double quote, it will be treated as a single value (instead of 5 different values),
as shown in the example below.

$ cat [Link]
i=1
for day in "Mon Tue Wed Thu Fri"
do
echo "Weekday $((i++)) : $day"
done

$ ./[Link]
Weekday 1 : Mon Tue Wed Thu Fri
2. Variable for the list after “in” keyword

Instead of providing the values directly in the for loop, you can store the values in a variable, and
use the variable in the for loop after the “in” keyword, as shown in the following example.

$ cat [Link]
i=1
weekdays="Mon Tue Wed Thu Fri"
for day in $weekdays
do
echo "Weekday $((i++)) : $day"
done

$ ./[Link]
Weekday 1 : Mon
29

Weekday 2 : Tue
Weekday 3 : Wed
Weekday 4 : Thu
Weekday 5 : Fri

Method 2:

Method 2: for loop using C program syntax

This example uses the 2nd method of bash for loop, which is similar to the C for loop syntax.
The following example generates 5 random number using the bash C-style for loop.

for (( i=1; i <= 5; i++ ))


do
echo "Random number $i: $RANDOM"
done

$ ./[Link]
Random number 1: 23320
Random number 2: 5070
Random number 3: 15202
Random number 4: 23861
Random number 5: 23435

Until Loop
The while loop is perfect for a situation where you need to execute a set of commands while
some condition is true. Sometimes you need to execute a set of commands until a condition is
true.

Syntax:

until command
do
Statement(s) to be executed until command is true
done

Here Shell command is evaluated. If the resulting value is false, given statement(s) are executed.
If command is true then no statement would be not executed and program would jump to the
next line after done statement.

Example:

Here is a simple example that uses the until loop to display the numbers zero to nine:

#!/bin/sh
30

a=0

until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done

This will produce following result:

0
1
2
3
4
5
6
7
8
9

Loop Control
two statements used to control shell loops:

1. The break statement


2. The continue statement

The infinite Loop:

All the loops have a limited life and they come out once the condition is false or true depending
on the loop.

A loop may continue forever due to required condition is not met. A loop that executes forever
without terminating executes an infinite number of times. For this reason, such loops are called
infinite loops.

Example:

Here is a simple example that uses the while loop to display the numbers zero to nine:

a=10
31

while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done

This loop would continue forever because a is alway greater than 10 and it would never become
less than 10. So this true example of infinite loop.

The break statement:

The break statement is used to terminate the execution of the entire loop, after completing the
execution of all of the lines of code up to the break statement. It then steps down to the code
following the end of the loop.

Syntax:

The following break statement would be used to come out of a loop:

Break

The break command can also be used to exit from a nested loop using this format:
break n

Here n specifies the nth enclosing loop to exit from.

Example:

Here is a simple example which shows that loop would terminate as soon as a becomes 5:

a=0

while [ $a -lt 10 ]
do
echo $a
if [ $a -eq 5 ]
then
break
fi
a=`expr $a + 1`
done

This will produce following result:

0
1
2
32

3
4
5

Here is a simple example of nested for loop. This script breaks out of both loops if var1 equals 2
and var2 equals 0:

for var1 in 1 2 3
do
for var2 in 0 5
do
if [ $var1 -eq 2 -a $var2 -eq 0 ]
then
break 2
else
echo "$var1 $var2"
fi
done
done

This will produce following result. In the inner loop, you have a break command with the
argument 2. This indicates that if a condition is met you should break out of outer loop and
ultimately from inner loop as well.

10
15

The continue statement:

The continue statement is similar to the break command, except that it causes the current
iteration of the loop to exit, rather than the entire loop.

This statement is useful when an error has occurred but you want to try to execute the next
iteration of the loop.

Syntax:

continue

Like with the break statement, an integer argument can be given to the continue command to
skip commands from nested loops.

continue n

Here n specifies the nth enclosing loop to continue from.

Example:
33

The following loop makes use of continue statement which returns from the continue statement
and start processing next statement:

NUMS="1 2 3 4 5 6 7"

for NUM in $NUMS


do
Q=`expr $NUM % 2`
if [ $Q -eq 0 ]
then
echo "Number is an even number!!"
continue
fi
echo "Found odd number"
done

This will produce following result:

Found odd number


Number is an even number!!
Found odd number
Number is an even number!!
Found odd number
Number is an even number!!
Found odd number

Arrays:
An array is a variable containing multiple values may be of same type or of different type. There
is no maximum limit to the size of an array, nor any requirement that member variables be
indexed or assigned contiguously. Array index starts with zero.

Declaring an Array and Assigning values

In bash, array is created automatically when a variable is used in the format like,

name[index]=value
 name is any name for an array
 index could be any number or expression that must evaluate to a number greater than or
equal to zero

Example :

Unix[0]='Debian'
Unix[1]='Red hat'
Unix[2]='Ubuntu'
Unix[3]='Suse'
34

echo ${Unix[1]}

O/P
$./[Link]
Red hat

Initializing an array during declaration

Instead of initializing an each element of an array separately, you can declare and initialize an
array by specifying the list of elements (separated by white space) with in a curly braces.

Syntax:
arrayname=(element1 element2 element3)

Example :

Define array called distro with 3 elements:

distro=("redhat" "debian" "gentoo")

How do reference any element in bash array?

Any element of an array may be referenced using following syntax:

${ArrayName[subscript]}

To print first element and second element:

echo ${distro[0]} # will print red hat

echo ${distro[2]} # will print gentoo

Length of the Bash Array

We can get the length of an array using the special parameter called $#.

${#arrayname[@]} gives you the length of the array.

Example :

array=(red green blue yellow magenta)


len=${#array[*]}
35

echo "The array has $len members. They are:"


i=0
while [ $i -lt $len ]; do
echo "$i: ${array[$i]}"
let i++
done
O/P :

The array has 5 members. They are:


0: red
1: green
2: blue
3: yellow
4: magenta

Length of the nth Element in an Array

${#arrayname[n]} should give the length of the nth element in an array.

Unix[0]='Debian'
Unix[1]='Red hat'
Unix[2]='Ubuntu'
Unix[3]='Suse'

echo ${#Unix[3]} # length of the element located at index 3 i.e Suse

O/P : 4

Extraction by offset and length for an array

The following example shows the way to extract 2 elements starting from the position 3 from an
array called Unix.

$cat [Link]
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[@]:3:2}

$./[Link]
Suse Fedora

The above example returns the elements in the 3rd index and fourth index. Index always starts
with zero.
36

Extraction with offset and length, for a particular element of an array

To extract only first four elements from an array element . For example, Ubuntu which is located
at the second index of an array, you can use offset and length for a particular element of an array.

$cat [Link]
#! /bin/bash

Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');


echo ${Unix[2]:0:4}

./[Link]
Ubun

The above example extracts the first four characters from the 2nd indexed element of an array.

Search and Replace in an array elements

The following example, searches for Ubuntu in an array elements, and replace the same with the
word ‘SCO Unix’.

$cat [Link]
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');

echo ${Unix[@]/Ubuntu/SCO Unix}

$./[Link]
Debian Red hat SCO Unix Suse Fedora UTS OpenLinux

In this example, it replaces the element in the 2nd index ‘Ubuntu’ with ‘SCO Unix’. But this
example will not permanently replace the array content.

Add an element to an existing Bash Array

The following example shows the way to add an element to the existing array.

$cat [Link]
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Unix=("${Unix[@]}" "AIX" "HP-UX")
echo ${Unix[7]}

$./[Link]
AIX
37

In the array called Unix, the elements ‘AIX’ and ‘HP-UX’ are added in 7th and 8th index
respectively.

Remove an Element from an Array

unset is used to remove an element from an [Link] will have the same effect as assigning
null to an element.

$cat [Link]
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');

unset Unix[3]
echo ${Unix[3]}

The above script will just print null which is the value available in the 3rd index. The following
example shows one of the way to remove an element completely from an array.

$ cat [Link]
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
pos=3
Unix=(${Unix[@]:0:$pos} ${Unix[@]:$(($pos + 1))})
echo ${Unix[@]}

$./[Link]
Debian Red hat Ubuntu Fedora UTS OpenLinux

In this example, ${Unix[@]:0:$pos} will give you 3 elements starting from 0th index i.e 0,1,2
and ${Unix[@]:4} will give the elements from 4th index to the last index. And merge both the
above output. This is one of the workaround to remove an element from an array.

Copying an Array

Expand the array elements and store that into a new array as shown below.

#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Linux=("${Unix[@]}")
echo ${Linux[@]}

$ ./[Link]
Debian Red hat Ubuntu Fedora UTS OpenLinux
38

Concatenation of two Bash Arrays

Expand the elements of the two arrays and assign it to the new array.

$cat [Link]
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh');

UnixShell=("${Unix[@]}" "${Shell[@]}")
echo ${UnixShell[@]}
echo “Length of new array:”${#UnixShell[@]}

$ ./[Link]
Debian Red hat Ubuntu Suse Fedora UTS OpenLinux bash csh jsh rsh ksh rc tcsh
Length of new array :14

Load Content of a File into an Array

You can load the content of the file line by line into an array.

Create file with the below contents:

Welcome
to
thegeekstuff
Linux
Unix

Example :
filecontent=( `cat "logfile" `)

for t in "${filecontent[@]}"
do
echo $t
done
echo "Read file content!"

O/P:
Welcome
to
thegeekstuff
Linux
39

Unix
Read file content!

In the above example, each index of an array element has printed through for loop.

Deleting array variables

The unset built-in is used to destroy arrays or member variables of an array:


[bob in ~] unset ARRAY[1]

[bob in ~] echo ${ARRAY[*]}


one three four

[bob in ~] unset ARRAY

[bob in ~] echo ${ARRAY[*]}


<--no output-->

String Manipulation – Length, Substring, Find and Replace

In bash shell, when you use a dollar sign followed by a variable name, shell expands the variable
with its value. This feature of shell is called parameter expansion.
But parameter expansion has numerous other forms which allow you to expand a parameter and
modify the value or substitute other values in the expansion process. In this article, let us review
how to use the parameter expansion concept for string manipulation operations.

1. Identify String Length

There are several ways to get length of the string.

Method1

 The simplest one is ${#varname}, which returns the length of the value of the variable as
a character string.

Example:
var="Welcome to the geekstuff"
echo ${#var}
40

O/P: 24

Method2

expr length $string

Example :
String="Welcome to the geekstuff"
expr length $string

2. Extract a Substring from a Variable inside Bash Shell Script

Bash provides a way to extract a substring from a string. The following example expains how to
parse n characters starting from a particular position.

Method 1:
${string:position}
Extract substring from $string at $position
Method 2:
${string:position:length}

Extract $length of characters substring from $string starting from $position. In the below
example, first echo statement returns the substring starting from 15th position. Second echo
statement returns the 4 characters starting from 15th position. Length must be the number greater
than or equal to zero.
Example;
var="Welcome to the geekstuff"
echo ${var:15}
echo ${var:15:4}

O/P
geekstuff
geek
41

3. Shortest Substring Match or Substring Removal


Method 1:
Following syntax deletes the shortest match of $substring from front of $string
${string#substring}

Method 2:
Following syntax deletes the shortest match of $substring from back of $string
${string%substring}

Example :
foo="this is a test"
echo ${foo#t*is} or echo ${foo#* } or echo ${filename#*.}

echo ${foo%t*st} or echo ${foo% *}or echo ${filename%.*}

O/P
After deletion of shortest match from front: is a test
After deletion of shortest match from back: this is a

In the first echo statement substring ‘*.’ matches the characters and a dot, and # strips from the
front of the string, so it strips the substring “bash.” from the variable called filename. In second
echo statement substring ‘.*’ matches the substring starts with dot, and % strips from back of the
string, so it deletes the substring ‘.txt’

4. Longest Substring Match


Method 1:
Following syntax deletes the longest match of $substring from front of $string
${string##substring}
Method 2:
Following syntax deletes the longest match of $substring from back of $string
${string%%substring}
42

Example:
filename="[Link]"

echo "After deletion of longest match from front:" ${filename##*.}


echo "After deletion of longest match from back:" ${filename%%.*}

O/P:
After deletion of longest match from front: txt
After deletion of longest match from back: bash

In the above example, ##*. strips longest match for ‘*.’ which matches “[Link].” so after
striping this, it prints the remaining txt. And %%.* strips the longest match for .* from back
which matches “.[Link]”, after striping it returns “bash”.

5. Find and Replace String Values

1. Replace only first match


${string/pattern/replacement}
It matches the pattern in the variable $string, and replace only the first match of the pattern with
the replacement.
Example:

filename="[Link]"
echo "After Replacement:" ${filename/string./operations.}

O/P : After Replacement: [Link]

2. Replace all the matches


${string//pattern/replacement}
It replaces all the matches of pattern with replacement.
43

Example:
filename="Path of the bash is /bin/bash"
echo "After Replacement:" ${filename//bash/sh}

O/P:
After Replacement: Path of the sh is /bin/sh

3. Replace beginning and end

Method 1:
${string/#pattern/replacement
Following syntax replaces with the replacement string, only when the pattern matches beginning
of the $string.

Method 2:
${string/%pattern/replacement
Following syntax replaces with the replacement string, only when the pattern matches at the end
of the given $string.

Example:
filename="/root/admin/monitoring/[Link]"

echo "Replaced at the beginning:" ${filename/#\/root/\/tmp}


echo "Replaced at the end": ${filename/%.*/.ksh}

O/P:
Replaced at the beginning: /tmp/admin/monitoring/[Link]
Replaced at the end: /root/admin/monitoring/[Link]

6. Index
44

Syntax: expr index $string $substring

Function index return the position of substring in string counting from one.
Example :
Enter the String: Ridhanya

Enter the Character to find its index: d


3

You might also like