Shell Scripting
Shell Scripting
Shell Scripting
What is Shell?
Shell is a user program that provides a Command Line Interpreter (C.L.I.) that carries the user-
typed, human-readable input commands from the terminal and converts them into instructions the
kernel can understand. The shell examines the commands and communicates the actions for each
user logged in to the kernel.
The shell used in Unix-based operating systems such as Linux, macOS, and others is called Unix
Shell. However, the term “shell” can describe any command line interface type, and “Unix shell”
refers exclusively to the shells used in Unix-based operating systems.
These Unix shells include several implementations with syntax and features such as command
execution, file manipulation, process management, and input/output redirection.
There are several Unix shells available with unique features for different user needs, with the most
common ones being:
• Bourne Shell (sh): It is the first Unix shell that was introduced in 1970. It offers basic
functionality and scripting capabilities like list files, read inputs, variables, and control
flow statements.
• Bash (Bourne Again Shell): It is the advanced version of Bourne Shell that offers
additional features like command line editing and improved scripting capabilities.
• C Shell (csh): It is known for its C-like syntax and interactive features such as history
substitution.
• Korn Shell (ksh): It was developed by David Korn as an extension of the Bourne Shell,
which provides advanced scripting capabilities and is backward compatible with Bourne
Shell(sh).
• Z Shell (zsh): It is highly customizable with advanced features like tab completion, spelling
correction, and extensive configuration options.
• Fish (Friendly Interactive Shell): fish (stylized in lowercase) is a Unix shell focusing more
on interactivity and usability with features like syntax highlighting, auto suggestions, and
an intuitive scripting language.
A shell script is a set of instructions crafted to be executed by an operating system, which serves
as a command line interpreter for automating tasks and executing commands.
shells are interactive, which means they accept commands as input from users and execute them.
However, sometimes we want to execute a bunch of commands routinely, so we have to type in all
commands each time in the terminal.
As a shell can also take commands as input from file, we can write these commands in a file and
can execute them in shell to avoid this repetitive work. These files are called Shell Scripts or Shell
Programs. Shell scripts are similar to the batch file in MS-DOS. Each shell script is saved
with `.sh` file extension e.g., myscript.sh.
A shell script has syntax just like any other programming language. If you have any prior
experience with any programming language like Python, C/C++ etc. It would be very easy to get
started with it.
• Functions
Common tasks carried out by shell scripts involve handling files, running programs, and
generating text output. Essentially, they provide a streamlined way to automate diverse
operations within the operating system.
Unix shell scripting refers to shell scripting done specifically within Unix-based operating
systems. It is a subset of shell scripting explicitly done in a Unix environment.
Before we delve further into shell scripting, knowing a few terminologies related to the shell is
essential as it will help us understand it better.
• Terminal: An interface to access the shell. It lets users submit commands and see the
results.
• Shell Shebang Line: The first line of a shell script (usually starting with #!) specifies the
interpreter for executing the script. For example, #!/bin/bash indicates the Bash interpreter
should run the script.
• Shell Comments: Comments are lines that the shell interpreter ignores. They are used to
explain the script’s purpose or specific sections. Lines starting with (#) are comments.
• Shell Variables: Variables are named storage locations with values used within the script.
We can assign values to variables using the = operator and then reference them later in
the script using the variable name preceded by a dollar sign (e.g., $name).
• Shell Sourcing a File: Sourcing a file in a shell means executing the commands in the file
in the current shell environment. When you source a file, the commands are executed the
same way they had been typed directly into the shell. A file is sourced by writing
source <fileName> or ./ <fileName> in the command line. For example, if we want to
execute the commands written in helloworld.sh, We can source it into the terminal
with ./helloworld.sh.
• Commands: Shell scripts are essentially lists of commands, the building blocks. These are
built-in keywords (like cd to change directory, echo to print text) or commands for external
programs.
• Redirection: This allows us to control the input and output of commands within the script.
Operators like > (redirect output), >> (append to output), < (redirect input) are used for this
purpose.
• Control Flow Statements: These statements dictate the script’s execution order. Common
control flow statements include:
• System monitoring
• The command and syntax are exactly the same as those directly entered in the command line, so
programmers do not need to switch to entirely different syntax
• Quick start
• Prone to costly errors, a single mistake can change the command which might be harmful.
UNIX Editors
Any UNIX system provides many different editors out of the box. In
ed
ed is the original UNIX text editor, and it’s the most basic you can work with. It’s also very rarely used,
if ever used, by most people.
Run it by typing ed. This starts an interactive session. Enter in write mode by typing a on a single line,
and press enter. Then type everything you want, and once you are done, write just a dot (.) on a line and
press enter.
Now type w followed by a file name to save the buffer to a file. It will return the number of bytes written to
the file.
You can edit a file with ed by invoking it with the file name: ed <filename>. When you press a to add, you
add content to the bottom of the file.
Inside an ed session you can type ,p to print the current file content.
vi / vim
vim is a very popular file editor, especially among programmers. It’s actively developed and frequently
updated, and there’s a very big community around it. There’s even a Vim conference!
vi test.txt
You have to know that Vim has 2 main modes:
• insert mode
When you start the editor, you are in command mode. You can’t enter text like you expect from a GUI-
based editor. You have to enter insert mode. You can do this by pressing the i key. Once you do so,
the -- INSERT -- word appear at the bottom of the editor:
Now you can start typing and filling the screen with the file contents:
You can move around the file with the arrow keys, or using the h - j - k - l keys. h-l for left-right, j-k for
down-up.
Once you are done editing you can press the esc key to exit insert mode, and go back to command
mode.
At this point you can navigate the file, but you can’t add content to it (and be careful which keys you press
as they might be commands).
One thing you might want to do now is saving the file. You can do so by pressing : (colon), then w.
You can undo and edit by going to command mode and pressing u. You can redo (cancel an undo) by
pressing ctrl-r.
Those are the basics of working with Vim. From here starts a rabbit hole we can’t go into in this little
introduction.
I will only mention those commands that will get you started editing with Vim:
• go to the first character of a word and press d followed by w to delete that word. If you follow it
with e instead of w, the white space before the next word is preserved
• use a number between d and w to delete more than 1 word, for example use d3w to delete 3
words forward
• press d followed by d to delete a whole entire line. Press d followed by $ to delete the entire line
from where the cursor is, until the end
emacs
emacs is an awesome editor and it’s historically regarded as the editor for UNIX systems.
Famously vi vs emacs flame wars and heated discussions caused many unproductive hours for
developers around the world.
macOS users, stop a second now. If you are on Linux there are no problems, but macOS does not ship
applications using GPLv3, and every built-in UNIX command that has been updated to GPLv3 has not
been updated. While there is a little problem with the commands I listed up to now, in this case using an
emacs version from 2007 is not exactly the same as using a version with 12 years of improvements and
change. This is not a problem with Vim, which is up to date. To fix this, run brew install emacs and
running emacs will use the new version from Homebrew (make sure you have Homebrew installed)
You can start editing and once you are done, press ctrl-x followed by ctrl-w. You confirm the folder:
and Emacs tell you the file exists, asking you if it should overwrite it:
Answer y, and you get a confirmation of success:
You can exit Emacs pressing ctrl-x followed by ctrl-c. Or ctrl-x followed by c (keep ctrl pressed).
There is a lot to know about Emacs. More than I am able to write in this little introduction. I encourage you
to open Emacs and press ctrl-h r to open the built-in manual and ctrl-h t to open the official tutorial.
nano
You can directly type characters into the file without worrying about modes.
You can quit without editing using ctrl-X. If you edited the file buffer, the editor will ask you for
confirmation and you can save the edits, or discard them. The help at the bottom shows you the keyboard
commands that let you work with the file:
pico is more or less the same, although nano is the GNU version of pico which at some point in history
was not open source and the nano clone was made to satisfy the GNU operating system license
requirements.
o Open the terminal. Go to the directory where you want to create your script.
Note: In the last step you have to mention the path of the script if your script is in other directory.
First of all, create a simple script in any editor or with echo. Then we'll make it executable with chmod
+x command. To find the script you have to type the script path for the shell.
Look at the above snapshot, script echo Hello World is created with echo command
as hello_world. Now command chmod +x hello_world is passed to make it executable. We have given
the command ./hello_world to mention the hello_world path. And output is displayed.
Any line starting with a hash (#) becomes comment. Comment means, that line will not take part in script
execution. It will not show up in the output.
Shell Variables
A shell variable is a character string in a shell that stores some value. It could be an integer, filename,
string, or some shell command itself. Basically, it is a pointer to the actual data stored in memory. We
have a few rules that have to be followed while writing variables in the script (which will be discussed in
the article). Overall knowing the shell variable scripting leads us to write strong and good shell scripts.
A variable name could contain any alphabet (a-z, A-Z), any digits (0-9), and an underscore ( _ ).
However, a variable name must start with an alphabet or underscore. It can never start with a number.
Following are some examples of valid and invalid variable names:
ABC
_AV_3
AV232
Note: It must be noted that no other special character except underscore can be used in a variable name
because all other special characters have special meanings in Shell Scripting.
Defining Variables
Syntax
Example
num="1"
name="Devil"
These kinds of variables are scalar variables as they could hold one value at a time.
1) Accessing variable
Variable data could be accessed by appending the variable name with ‘$’ as follows:
#!/bin/bash
VAR_1="Devil"
VAR_2="OWL"
echo "$VAR_1$VAR_2"
Output:
DevilOWL
Unsetting Variables
The unset command directs a shell to delete a variable and its stored data from list of variables. It can be
used as follows:
#!/bin/bash
var1="Devil"
var2=23
echo $var1 $var2
unset var1
Output:
DEVIL 23
23
Note: The unset command could not be used to unset read-only variables.
These variables are read only i.e., their values could not be modified later in the script. Following is an
example:
#!/bin/bash
var1="Devil"
var2=23
readonly var1
echo $var1 $var2
var1=23
echo $var1 $var2
Output:
Devil 23
./bash1: line 8: var1: readonly variable
Devil 23
Variable Types
1) Local Variable:
Variables which are specific to the current instance of shell. They are basically used within the shell, but
not available for the program or other shells that are started from within the current shell.
For example:
`name=Jayesh`
In this case the local variable is (name) with the value of Jayesh. Local variables is temporary storage of
data within a shell script.
2) Environment Variable:
These variables are commonly used to configure the behavior script and programs that are run by shell.
Environment variables are only created once, after which they can be used by any user.
For example:
`export PATH=/usr/local/bin:$PATH` would add `/usr/local/bin` to the beginning of the shell’s search path
for executable programs.
3) Shell Variables:
Variables that are set by shell itself and help shell to work with functions correctly. It contains both, which
means it has both, some variables are Environment variable, and some are Local Variables.
For example:
`$SHELL` = Stores the path to the shell program that is being used.
A variable in a shell script is a means of referencing a numeric or character value. A shell script doesn’t
require you to declare a type for your variables.
These are the variables which are created and maintained by Linux (operating system) itself. Generally,
these variables are defined in CAPITAL LETTERS.
To print the value of above variables, use echo command: #echo $HOME #echo $USERNAME
User variables can be any text string of up to 20 letters, digits or an underscore character.
User variables are case sensitive, so the variable Var1 is different from the variable var1.
No spaces can appear between the variable, the equal sign, and the value. e.g. var1 = 10 var2 = -57 var3
= testing
The shell script automatically determines the data type used for the variable value. Variables defined
within the shell script maintain their values throughout the life of the shell script but are deleted when the
shell script completes.
Just like system variables, user variables can be referenced using the dollar sign.
System Calls:
A system call is the programmatic way in which a computer program requests a service from the kernel of
the operating system it is executed on. A system call is a way for programs to interact with the operating
system. A computer program makes a system call when it makes a request to the operating system’s
kernel. System call provides the services of the operating system to the user programs via Application
Program Interface (API). It provides an interface between a process and operating system to allow user-
level processes to request services of the operating system.System calls are the only entry points into the
kernel system. All programs needing resources must use system calls.
• Protection
• Networking, etc.
• Device management
• Information maintenance
• Communication
System calls in Unix are used for file system control, process control, interprocess communication etc.
Access to the Unix kernel is only available through these system calls. Generally, system calls are similar
to function calls, the only difference is that they remove the control from the user process.
There are around 80 system calls in the Unix interface currently. Details about some of the important
ones are given as follows -
access() This checks if a calling process has access to the required file
chdir() The chdir command changes the current directory of the system
kill() This system call sends kill signal to one or more processes
link() A new file name is linked to an existing file using link system call.
pause() The pause call suspends a file until a particular signal occurs.
System Call Description
alarm() The alarm system call sets the alarm clock of a process
Pipes in UNIX
The novel idea of Pipes was introduced by M.D Mcllroy in June 1972– version 2, 10 UNIX installations.
Piping is used to give the output of one command (written on LHS) as input to another command (written
on RHS). Commands are piped together using vertical bar “ | ” symbol. Syntax:
command 1|command 2
Example:
• Input: ls|more
• Output: more command takes input from ls command and appends it to the standard output. It
displays as many files that fit on the screen and highlighted more at the bottom of the screen. To
see the next screen hit enter or space bar to move one line at a time or one screen at a time
respectively.
Filters in UNIX
In UNIX/Linux, filters are the set of commands that take input from standard input stream i.e. stdin,
perform some operations and write output to standard output stream i.e. stdout. The stdin and stdout can
be managed as per preferences using redirection and pipes. Common filter commands
are: grep, more, sort.
1. grep Command:It is a pattern or expression matching command. It searches for a pattern or regular
expression that matches in files or directories and then prints found matches.
Syntax:
Example:
Output : searches hello in the ist_file.txt and outputs/returns the lines containing 'hello'.
Example:
Syntax:
$sort[options] filename
Example:
$sort fruits.txt
$sort -n grades.txt
3. more Command: It is used to customize the displaying contents of file. It displays the text file contents
on the terminal with paging controls. Following key controls are used:
• To quit, press q.
Syntax:
$more[options] filename
Example:
You can control the execution of Linux commands in a shell script with control structures. Control
structures allow you to repeat commands and to select certain commands over others. A control structure
consists of two major components: a test and commands. If the test is successful, then the commands
are executed. In this way, you can use control structures to make decisions as to whether commands
should be executed.
There are two different kinds of control structures: loops and conditions. A loop repeats commands,
whereas a condition executes a command when certain conditions are met. The BASH shell has three
loop control structures: while, for, and for-in. There are two condition structures: if and case. The control
structures have as their test the execution of a Linux command. All Linux commands return an exit status
after they have finished executing. If a command is successful, its exit status will be 0. If the command
fails for any reason, its exit status will be a positive value referencing the type of failure that occurred. The
control structures check to see if the exit status of a Linux command is 0 or some other value. In the case
of the if and while structures, if the exit status is a zero value, then the command was successful and the
structure continues.
Test Operations
With the test command, you can compare integers, compare strings, and even perform logical
operations. The command consists of the keyword test followed by the values being compared,
separated by an option that specifies what kind of comparison is taking place. The option can be thought
of as the operator, but it is written, like other options, with a minus sign and letter codes. For example, -
eq is the option that represents the equality comparison. However, there are two string operations that
actually use an operator instead of an option. When you compare two strings for equality you use the
equal sign, =. For inequality you use !=. Table 8-6 lists some of the commonly used options and operators
used by test. The syntax for the test command is shown here:
-gt Greater-than
Table 8-6: BASH Shell Test Operators
-lt Less-than
-ge Greater-than-or-equal-to
-le Less-than-or-equal-to
-eq Equal
-ne Not-equal
String Comparisons
= Equal strings
!= Not-equal strings
Logical Operations
-a Logical AND
-o Logical OR
! Logical NOT
File Tests
-r File is readable
-x File is executable
1. if statement
2. if-else statement
4. if..then..else..if..then..fi..fi..(Nested if)
5. switch statement
if statement
This block will process if specified condition is true.
Syntax:
if [ expression ]
then
statement
fi
if-else statement
If specified condition is not true in if part then else part will be execute.
Syntax
if [ expression ]
then
statement1
else
statement2
fi
then
statement1
statement2
elif [ expression2 ]
then
statement3
statement4
else
statement5
fi
if..then..else..if..then..fi..fi..(Nested if)
Nested if-else block can be used when, one condition is satisfies then it again checks another
condition. In the syntax, if expression1 is false then it processes else part, and again expression2
will be check.
Syntax:
if [ expression1 ]
then
statement1
statement2
else
if [ expression2 ]
then
statement3
fi
fi
switch statement
case statement works as a switch statement if specified value match with the pattern then it will
execute a block of that particular pattern
When a match is found all of the associated statements until the double semicolon (;;) is
executed.
A case will be terminated when the last command is executed.
If there is no match, the exit status of the case is zero.
Syntax:
case in
esac
Example Programs
Example 1:
Implementing if statement
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
fi
Output
$bash -f main.sh
a is not equal to b
Example 2:
Implementing if.else statement
a=20
b=20
if [ $a == $b ]
then
else
Output
$bash -f main.sh
a is equal to b
Example 3:
Implementing switch statement
CARS="bmw"
case "$CARS" in
#case 1
#case 2
#case 3
esac
Output
$bash -f main.sh
There are total 3 looping statements that can be used in bash programming
To alter the flow of loop statements, two commands are used they are,
1. break
2. continue
Here the command is evaluated and based on the resulting loop will execute, if the command is
raised to false then the loop will be terminated that.
#/bin/bash
while <condition>
do
<command 1>
<command 2>
<etc>
done
First, we create a file using a text editor in Linux. In this case, we are using `vim`editor.
vim looping.sh
Then we make our script executable using the `chmod` command in Linux.
chmod +x looping.sh
#/bin/bash
a=0
Output:
Explanation:
• #/bin/bash: Specifies that the script should be interpreted using the Bash shell.
• while [ $a -lt 10 ]: Initiates a while loop that continues as long as the value a is less than
10.
• a=expr $a + 1“: Increments the value of a by 1. The expr command is used for arithmetic
expressions.
Each time the for loop executes, the value of the variable var is set to the next word in the list of
words, word 1 to word N.
Syntax:
#/bin/bash
for <var> in <value1 value2 ... valuen>
do
<command 1>
<command 2>
<etc>
done
First, we create a file using a text editor in Linux. In this case, we are using `vim`editor.
vim for.sh
Then we make our script executable using the `chmod` command in Linux.
chmod +x for.sh
#/bin/bash
for a in 1 2 3 4 5 6 7 8 9 10
do
Output:
Explanation:
• #/bin/bash: Specifies that the script should be interpreted using the Bash shell.
• for a in 1 2 3 4 5 6 7 8 9 10: Initiates a for loop that iterates over the values 1 through 10,
assigning each value to the variable a in each iteration.
o then: Marks the beginning of the conditional block to be executed if the condition
is true.
• echo "Iteration no $a": Prints a message indicating the current iteration number.
The script sets up a for loop that iterates over the values 1 through 10. During each iteration, it
checks if the value a is equal to 5. If it is, the loop is exited using the break statement. Otherwise,
it prints a message indicating the current iteration number. The loop continues until it completes
all iterations or until it encounters a break statement.
First, we create a file using a text editor in Linux. In this case, we are using `vim`editor.
vim for_continue.sh
Then we make our script executable using the `chmod` command in Linux.
chmod +x for_continue.sh
#/bin/bash
for a in 1 2 3 4 5 6 7 8 9 10
do
if [ $a == 5 ]
then
continue
fi
echo “Iteration no $a”
done
Output:
Explanation:
• #/bin/bash: Specifies that the script should be interpreted using the Bash shell.
• for a in 1 2 3 4 5 6 7 8 9 10: Initiates a for loop that iterates over the values 1 through 10,
assigning each value to the variable a in each iteration.
o then: Marks the beginning of the conditional block to be executed if the condition
is true.
o continue: Skips the rest of the loop’s body and goes to the next iteration if
the condition is true.
• echo "Iteration no $a": Prints a message indicating the current iteration number. This line
is skipped if a is equal to 5 due to the continue statement.
The script sets up a for loop that iterates over the values 1 through 10. During each iteration, it
checks if the value a is equal to 5. If it is, the loop continues to the next iteration without executing
the remaining statements in the loop’s body. Otherwise, it prints a message indicating the current
iteration number. The loop continues until it completes all iterations.
The until loop is executed as many times as the condition/command evaluates to false. The loop
terminates when the condition/command becomes true.
Syntax:
#/bin/bash
until <condition>
do
<command 1>
<command 2>
<etc>
done
First, we create a file using a text editor in Linux. In this case, we are using `vim`editor.
vim until.sh
Then we make our script executable using the `chmod` command in Linux.
chmod +x until.sh
#/bin/bash
a=0
until [ $a -gt 10 ]
do
Output:
Explanation:
• #/bin/bash: Specifies that the script should be interpreted using the Bash shell.
• until [ $a -gt 10 ]: Initiates a until loop that continues as long as the value a is not greater
than 10.
• a=expr $a + 1“: Increments the value of a by 1. The expr command is used for arithmetic
expressions.
Note: Shell scripting is a case-sensitive language, which means proper syntax has to be followed
while writing the scripts.
First, we create a file using a text editor in Linux. In this case, we are using `vim`editor.
vim color.sh
chmod +x color.sh
#/bin/bash
# the for loop continues until it reads all the values from the COLORS
Output:
For until in Linux
Explanation:
• Initialization of Colors:
o for COLOR in $COLORS: Initiates a for loop that iterates over each value in
the COLORS variable.
o Loop Body:
This example demonstrates a simple for loop in Bash, iterating over a list of colors stored in
the COLORS variable. The loop prints a message for each color, indicating the current color being
processed. The loop iterates until all color values are exhausted.
First we create a file using a text editor in Linux. In this case we are using `vim`editor.
vim infinite.sh
chmod +x infinite.sh
#/bin/bash
while true
do
# Command to be executed
# sleep 1 indicates it sleeps for 1 sec
echo “Hi, I am infinity loop”
sleep 1
done
Output:
Explanation:
• while true: Initiates a while loop that continues indefinitely as the condition true is always
true.
o Loop Body:
o echo "Hi, I am infinity loop": Prints a message indicating that the script is
in an infinite loop.
o sleep 1: Pauses the execution of the loop for 1 second before the next
iteration.
• The loop continues indefinitely, executing the commands within its body repeatedly.
This example showcases the creation of an infinite loop using the while true construct in Bash.
The loop continuously prints a message indicating its status as an infinite loop and includes a
sleep 1 command, causing a one-second delay between iterations. Infinite loops are often used
for processes that need to run continuously until manually interrupted.
3. Interactive Name Confirmation Loop
First we create a file using a text editor in Linux. In this case we are using `vim`editor.
vim name.sh
chmod +x name.sh
#/bin/bash
CORRECT=n
while [ “$CORRECT” == “n” ]
do
# loop discontinues when you enter y i.e., when your name is correct
# -p stands for prompt asking for the input
Output:
Explanation:
• Initialization:
o CORRECT=n: Initializes a variable named CORRECT with the value “n”. This
variable is used to control the loop.
• Interactive Loop:
o while [ "$CORRECT" == "n" ]: Initiates a while loop that continues as long as the
value of CORRECT is equal to “n”.
o Loop Body:
o read -p "Enter your name:" NAME: Prompts the user to enter their
name and stores the input in the NAME variable.
o read -p "Is ${NAME} correct? " CORRECT: Asks the user to confirm
if the entered name is correct and updates the CORRECT variable
accordingly.
o The loop continues until the user enters “y” (indicating the correct name).
This example demonstrates an interactive loop that prompts the user to enter their name and then
asks for confirmation. The loop continues until the user confirms that the entered name is correct
by inputting “y”. The loop utilizes the `read` command for user input and updates the
`CORRECT` variable to control the loop flow.
Shell scripting is a powerful tool used to automate tasks in Unix-like operating systems. A shell
serves as a command-line interpreter, and shell scripts often perform file manipulation, program
execution, and text output. Here, we’ll look into functions in shell scripting, exploring their
structure, usage, and types to help you effectively incorporate them into your scripts.
A function is a collection of statements that execute a specified task. Its main goal is to break
down a complicated procedure into simpler subroutines that can subsequently be used to
accomplish the more complex routine. For the following reasons, functions are popular:
• Code Reusability: Functions allow you to write code once and use it multiple times
throughout your script.
• Enhanced Readability: Functions help organize code into logical blocks, making scripts
easier to read and understand.
• Modularity: Functions enable modular programming, where different tasks are handled by
separate, self-contained functions.
• Ease of Maintenance: Functions simplify debugging and updating code since changes can
be made in one place rather than throughout the script.
function_name(){
• The function_name can be any valid string and the body can be any sequence of valid
statements in the scripting language.
• The body of the function can include any sequence of valid shell commands or
statements.
Example
Let us try to understand the concept of functions by looking at an example. The following is a
code to print all the prime numbers between a range [le,ri].
It consists of a function ‘is_prime()‘ which is used to check if the given number is a prime or not.
In this function, we use the variable ‘$1’ to access the first argument, which is the number itself. In
scripting languages, we can access arguments by ‘$i’, where ‘i’ is a number that signifies the
position of the argument.
read le
read ri
is_prime(){
if [ $1 -lt 2 ]; then
return
fi
ctr=0
for((i=2;i<$1;i++)){
ctr=$(( ctr +1 ))
fi
fi
for((i=le;i<=ri;i++)){
is_prime $i
printf "\n"
Types of Functions
The functions in shell scripting can be boxed into a number of categories. The following are some
of them:
The return keyword is used by the functions for this purpose. The following is one such function
used to calculate the average of the given numbers.
find_avg(){
len=$#
sum=0
for x in "$@"
do
sum=$((sum + x))
done
avg=$((sum/len))
return $avg
find_avg 30 40 50 60
printf "\n"
Output:
Explanation:
• The function ‘find_avg()’ calculates the average of the given numbers and returns it using
the ‘return’ statement.
• Since the ‘return’ value is restricted to integers between 0 and 255, it prints the result
using ‘$?’, which holds the exit status of the last executed command.
2. The functions that terminate the shell using the ‘exit’ keyword.
These functions use the ‘exit’ command to stop the shell entirely. This can be useful for error
handling or when a certain condition requires the script to stop immediately.
is_odd(){
x=$1
if [ $((x%2)) == 0 ]; then
echo "Invalid Input"
exit 1
else
fi
is_odd 64
Output:
Explanation: The function ‘is_odd()’ checks if a number is odd. If it’s even, it outputs an error
message and exits the script using ‘exit 1’.
These functions can modify the values of variables directly within their scope or globally if the
variables are defined outside the function.
a=1
increment(){
a=$((a+1))
return
increment
echo "$a"
Output:
Explanation: The function ‘increment()’ increases the value of the variable ‘a’ by 1, demonstrating
how functions can directly alter variable values.
These functions output information directly to the standard output using the ‘echo’ command.
hello_world(){
return
hello_world
Output:
Explanation: The function ‘hello_world()’ simply prints “Hello World” to the standard output,
illustrating a basic use of echoing in functions.
Regular Expressions
A regular expression (regex) is a text pattern that can be used for searching and replacing.
Regular expressions are similar to Unix wild cards used in globbing, but much more powerful, and
can be used to search, replace and validate text.
Regular expressions are used in many Unix commands such as find and grep, and also within
most programming languages such as R and Python.
We only show basic usage here to get you started. To get practice, first spend some time
at https://regex101.com to get a better understanding of how to use regular expressions, then find
out how to use them in your text editor to do a search and replace.
Matching characters
We will practice using grep. If a successful match is found, the line of text will be returned;
otherwise nothing.
In [1]:
In [2]:
abcd
In [3]:
abcd
No match for ac
In [4]:
In [5]:
abcd
In [6]:
abcd
In [8]:
In [9]:
abcd
In [10]:
a2b
In [11]:
a2b
In [12]:
a2b
In [13]:
In [14]:
a2b
In [15]:
echo a2b | grep [A-Z]
Exceptions
The ^ within a character set says match anything NOT in the set.
In [16]:
In [17]:
a2b
Many useful sets of characters (e.g. all digits) have been pre-defined as character classes that you
can use in your regular expressions. Character classes are a bit clumsy in the Unix shell, but
simpler forms are often used in programming languages (e.g. ‘:raw-latex:`\d`’ instead of ‘[:digit:]’).
In [18]:
a2b
In [19]:
a2b
In [20]:
In [21]:
a2,b
Alternative expressions
-E, --extended-regexp
In [22]:
cat
Without -E
In [23]:
cat
In [24]:
dog
In [25]:
In [26]:
fox
In [27]:
abcd
In [28]:
In [29]:
In [30]:
abcd
Repeating characters
• ‘{m, n}’ matches between m and n repeats of the preceding character set.
In [31]:
In [32]:
abbbcd
In [33]:
In [34]:
abbbcd
In [35]:
abbbcd
\< and \> indicate word boundaries. That is, \<foo\> will only match foo bar or bar foo but
not foobar or barfoo.
In [36]:
In [37]:
In [38]:
In [39]:
123_456_123_456
In [40]:
123_456_123_456
In [41]: