Introduction to Shell Scripting Basics
Introduction to Shell Scripting Basics
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
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.
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.
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 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."
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.
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.
$ 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 -x [Link]
whoever reads the file without the password will not be able to see the contents of the file.
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.
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:
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
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"
Variable Types:
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.
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.
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.
<<COMMENT
COMMENT
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
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:
Example
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.
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.
val=`expr 2 + 2`
echo "Total value : $val"
Total value : 4
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:
= Assignment - Assign right operand in left operand a=$b would assign value of b into a
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.
#!/bin/sh
a=10
b=20
val=`expr $a + $b`
11
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
a + b : 30
a - b : -10
a * b : 200
b/a:2
b%a:0
a is not equal to b
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".
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.
#!/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 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:
#!/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
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:
str Check if str is not the empty string. If it is empty [ $a ] is not false.
16
#!/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
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:
#!/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
If else statements are useful decision making statements which can be used to select an option
from a given set of options.
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.
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.
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
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.
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.
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
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
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
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.
Syntax:
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:
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.
$ ./[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
0
1
2
3
4
5
6
7
8
9
Loop Control
two statements used to control shell loops:
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 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:
Break
The break command can also be used to exit from a nested loop using this format:
break n
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
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 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
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"
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.
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
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 :
${ArrayName[subscript]}
We can get the length of an array using the special parameter called $#.
Example :
Unix[0]='Debian'
Unix[1]='Red hat'
Unix[2]='Ubuntu'
Unix[3]='Suse'
O/P : 4
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
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
./[Link]
Ubun
The above example extracts the first four characters from the 2nd indexed element of an array.
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');
$./[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.
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.
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
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
You can load the content of the file line by line into an array.
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.
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.
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
Example :
String="Welcome to the geekstuff"
expr length $string
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
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#*.}
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’
Example:
filename="[Link]"
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”.
filename="[Link]"
echo "After Replacement:" ${filename/string./operations.}
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
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]"
O/P:
Replaced at the beginning: /tmp/admin/monitoring/[Link]
Replaced at the end: /root/admin/monitoring/[Link]
6. Index
44
Function index return the position of substring in string counting from one.
Example :
Enter the String: Ridhanya