SHELL PROGRAMMING ON LINUX
I. Basic theory
1. Introduction to Shell
A shell is a command-line interface (CLI) that acts as a bridge between
the user and the operating system kernel. It enables users to enter
commands to perform tasks such as file management, program
execution, or automating workflows.
A shell command is an instruction given to the shell to perform an
operation, such as managing files, executing programs, or handling
system processes. Commands are executed in a terminal, and they
interact with the operating system through the shell. A shell command
generally follows this structure:
command [options] [arguments]
• command – The actual command to execute (e.g., ls, mkdir, echo).
• options – Modify the behavior of the command (usually preceded by
- or --).
• arguments – Specify what the command should act on (e.g., a
filename or directory).
Example shell command: ls
ls -l /home/user
1 Advanced Programming Techniques (ELT 3296)
• ls → Name of command (using to list directory contents).
• -l → The option (long format, showing details like permissions,
owner, size, etc.)
• /home/user → The argument (the directory to list).
Example of executing ls command on terminal
2. Some basic shell commands
2.1 File and directory management shell commands
The Linux file system is a structured way of organizing and storing files on
a disk. It follows a hierarchical directory structure, starting from the root
directory (/). Everything in Linux (files directories, devices, processes) is
represented as a file.
Linux filesystem organization
/ (Root Directory) → The top-level directory containing all files and
subdirectories.
2 Advanced Programming Techniques (ELT 3296)
/home → User directories (/home/user).
/bin → Essential binary programs (e.g., ls, cp, mv).
/etc → Configuration files for the system.
/var → Logs, caches, and variable data.
/tmp → Temporary files (cleared on reboot).
/dev → Device files (e.g., /dev/sda for hard drives).
To locate a file or a directory in Linux file system, we need an absolute
path or a relative path.
An absolute path is the full path to a file or directory, starting from the
root (/).
• Always begins with /.
• Points to the same location, regardless of the current directory.
Example: /home/jono is an absolute path, which locate user directory
jono in above Linux file system.
A relative path is a path relative to the current working directory.
• Does not start with /.
• Uses special symbols:
o . → Current directory
o .. → Parent directory
o
Example: If you are in /home/jono/ and you want to access subdirectory
work inside directory jono => ./work
In Linux, file access modes determine who can read, write, or execute a
file. These permissions are set for three categories of users and are
displayed using the ls -l command.
Each file in Linux has three types of users with different access
permissions:
Owner (User - u): The person who created the file.
Group (g): Users who belong to the same group as the owner.
Others (o): All other users who are not the owner or in the group.
3 Advanced Programming Techniques (ELT 3296)
Each file or directory has three types of permissions:
Symbol Permission Numeric Value Description
r Read 4 Can view file contents.
w Write 2 Can modify or delete
the file.
x Execute 1 Can run the file (if it's
a program/script).
Using the ls -l command to display file permissions:
Example: ls -l myfile.txt
Example output: -rw-r--r-- 1 user group 1234 Feb 15 12:00 myfile.txt
Owner (rw-) → Can read and write, but not execute.
Group (r--) → Can only read.
Others (r--) → Can only read.
File permissions can be modified using chmod command
Example: chmod u+x myfile.txt
=>This command adds execute mode access to owner of the file.
Below table summarizes the syntax and usage of basic shell commands
for file and directory management.
4 Advanced Programming Techniques (ELT 3296)
Command Syntax Description Example usage
cd cd [directory] Change cd
directory /home/user
/Documents
cd . . Move up one level cd ..
cd ~ Move to home cd ~
directory
cd - Go to the previous cd -
directory
pwd Pwd Print current directory pwd
path
mkdir mkdir [directory] Create a new directory mkdir
my_folder
mkdir -p Create nested mkdir -p
parent/child directories dir1/dir2/dir3
rmdir rmdir [directory] Remove an empty rmdir
directory my_folder
cp cp [source] Copy a file cp file.txt
[destination] /home/user/b
ackup/
cp -r [source] Copy a directory cp -r
[destination] my_folder
/home/user/b
5 Advanced Programming Techniques (ELT 3296)
ackup/
mv mv [source] Move a file or directory mv file.txt
[destination] /home/user/D
ocuments/
mv oldname.txt Rename a file mv old.txt
newname.txt new.txt
Rm rm [file] Remove a file rm file.txt
rm -r [directory] Remove a directory rm -r
and its contents my_folder
chmod chmod [OPTIONS] Modify read (r), write chmod u+x
MODE FILE (w), and execute (x) script.sh
permissions for the => Execute
owner (u), group (g), Permission to
and others (o). Owner
chmod
u=rw,g=r,o=
file.txt
=> Owner can
read & write,
group can
read, others
have no
access.
Cat cat [file] Display file content cat file.txt
6 Advanced Programming Techniques (ELT 3296)
cat > [file] Create a new file cat >
newfile.txt
(press Ctrl + D
to save)
cat >> [file] Append text to a file echo
"New line" >>
file.txt
Echo echo "text" Print text to the echo "Hello,
terminal World!"
echo "text" > Write text to a file echo "This is a
[file] test" > test.txt
echo Print an echo $HOME
$VARIABLE environment variable
2.2 Process management shell commands
Basic charateristics of a process
A process in Linux is an instance of a running program. Every command
or application executed in Linux runs as a process. The Linux kernel
manages processes, assigning system resources such as CPU time and
memory. Some process characteristics are:
Process ID (PID): A unique identifier assigned to each process.
Parent process ID (PPID): The PID of the parent process that created it.
User ID (UID) & Group ID (GID): Identifies the user and group that
owns the process.
Process state: The current state of the process (e.g., running, sleeping,
stopped).
CPU & Memory Usage: Amount of CPU time and memory the process
consumes.
7 Advanced Programming Techniques (ELT 3296)
The ps (process status) command is used to display information about
active processes running on a Linux system. It helps monitor process
details such as process ID (PID), parent process ID (PPID), CPU usage,
memory usage, and more.
Example of executing ps command in terminal
Redirecting processes in Linux
In Linux, process redirection allows you to control the input, output, and
error streams of a process. This is useful for saving output to a file, using
one command’s output as another’s input, or discarding unwanted output.
Table below covers types of redirection:
8 Advanced Programming Techniques (ELT 3296)
Command Type Description Example usage
> Output Redirection Redirects standard ls > files.txt
output (stdout) to a
file (overwrites if
exists).
>> Append Output Redirects output to echo "New
a file (appends line" >> log.txt
instead of
overwriting).
< Input Redirection Redirects input from sort <
a file instead of names.txt
keyboard input.
2> Error Redirection Redirects standard ls
error (stderr) to a non_existent_
file. dir 2> error.log
2>> Append Error Appends error rm
messages to a file. nonexistent_fil
e 2>> error.log
&> Redirect Output & Redirects both command &>
Error stdout and stderr to output.txt
a file.
9 Advanced Programming Techniques (ELT 3296)
Interprocess communication (IPC) in Linux
Interprocess Communication (IPC) is a mechanism that allows multiple
processes running on the same system (or different systems) to exchange
data and signals with each other. Since each process in Linux runs in its
own memory space, IPC provides ways for processes to share
information and synchronize their execution.
A pipe (|) in Linux is an interprocess communication (IPC) mechanism
that allows the output of one process to be used as input for another
process. Pipe are unidirectional (data flows in one direction).
Example: Find all file have extension “.txt” in current directory using grep,
ls and pipe
Grep command
The grep command in Linux is used for searching text or patterns within
files.
Syntax of grep:
grep [OPTIONS] PATTERN [FILE...]
• PATTERN: The search pattern (string or regex).
• FILE: The file(s) to search within.
• OPTIONS: Modify search behavior (e.g., case-insensitive, recursive
search).
Summary table of using grep
Option Description Example
grep "pattern" Search for "pattern" in grep "error" logfile.txt
file file
-i Case-insensitive search grep -i "error" logfile.txt
-n Show line numbers grep -n "error"
logfile.txt
10 Advanced Programming Techniques (ELT 3296)
-v Show lines not matching grep -v "error"
pattern logfile.txt
-w Match whole word only grep -w "error"
logfile.txt
-c Count grep -c "error"
occurrences logfile.txt
-r Search recursively in a grep -r "error"
directory /var/logs/
Example: Find all file have extension “.txt” in current directory
ls | grep ”.txt”
ls command lists all files in current directory and passes the output to
grep using pipe (|).Then, grep command filters only .txt files in current
directory.
A signal is a software-generated notification sent from one process to
another or from the OS to a process.
Signal Number Description
SIGKILL 9 Immediately terminates a
process (cannot be ignored).
SIGTERM 15 Requests a process to
terminate (default kill
signal).
SIGSTOP 19 Pauses (stops) a process.
11 Advanced Programming Techniques (ELT 3296)
SIGCONT 18 Resumes a stopped process.
SIGINT 2
Example: Sending signals using kill command
Find a process ID (PID): ps -e | grep firefox
Kill a process (sending SIGKILL signal to process): kill -9 <PID>
This forcefully stops the process
3. Introduction to shell script
Shell script is a collection of commands written in a text file to perform a
specific task. When executed, the shell reads and executes each line of
the script sequentially.
Popular shells:
● bash (Bourne Again Shell): The default shell on most Linux systems.
● sh (Bourne Shell): Simple and lightweight.
● zsh (Z Shell): Advanced features.
● csh (C Shell): Ideal for mathematical scripting.
3.1 Creating and Running Shell Scripts
Step 1. Create a script file
● Using a text editor like vim, nano or gedit.
● Example: vim scriptname.sh
● A script file with the name myscript.sh is created.
Step 2. Write commands in the script
Vim has three different modes:
Mode Usage How to enter
12 Advanced Programming Techniques (ELT 3296)
Normal Used for navigation and Press Esc
mode commands)
(default - Using arrow keys to move
mode) left, right, down, up
- Copy
+ yy: copy the current line
+ yw: copy the current word
- Paste
+ p: paste after cursor
+P: paste before cursor
- Delete:
+ dd: delete current line
+ dw: delete current word
- Undo and redo:
+ u: undo last change
+Ctrl plus r: redo last undone
change
Insert mode Type/edit text, can use arrow keys Press i or a
to navigate
Command Run Vim commands Press : from
mode - :w -> save the file normal mode
- :q -> quit Vim
- :wq or zz save and quit
- :q! ->quit without saving
- :!command -> run a shell
command in Vim (Ex: :!ls)
13 Advanced Programming Techniques (ELT 3296)
● The first line should start with #!/bin/bash to specify the shell
interpreter.
● Write commands to solve the problem.
● Save and exit the editor:
○ nano: Press CTRL + X, then Y, and Enter.
○ vim: Press ESC, type :wq, and press Enter.
Step 3. Grant execute permissions
● Using command: chmod +x [scriptname].sh
Step 4. Run the script
● Using command: ./[scriptname].sh
3.2. Basic Structure of Shell scripts
3.2.1 Variables in Shell
You don’t usually declare variables in the shell before using them.
Instead, you create them by simply using them (for example, when you
assign an initial value to them). You can access the contents of a variable
by preceding its name with a $. A string must be delimited by quote
marks if it contains spaces. In addition, there can’t be any spaces on
either side of the equals sign.
You can assign user input to a variable by using the read command and
use echo command to display value of a variable.
Example:
14 Advanced Programming Techniques (ELT 3296)
Environment variables and Parameter variables
When a shell script starts, some variables are initialized from values in the
environment. These are normally in all uppercase form to distinguish
them from user-defined (shell) variables in scripts, which are
conventionally lowercase.
Environment Variable Description
$HOME User's home directory.
$USER Current username.
$PWD Current working directory
$# The number of parameters passed
$0 The nameThe
of the shell
name ofscript
the shell scrip
15 Advanced Programming Techniques (ELT 3296)
If your script is invoked with parameters, some additional variables are
created. If no parameters are passed, the environment variable $# still
exists but has a value of 0.
The parameter variables are listed in the following table:
Parameter Variable Description
$1, $2,…. The parameters given to the script
$@ A list of all parameters
Example: Manipulating parameter and enviroment variables
Shell script Result
3.2.2 Conditions
Fundamental to all programming languages is the ability to test
conditions and perform different actions based on those decisions. The
condition types that you can use fall into three types: string comparision,
arithmetic comparison and file conditionals. The following tables
describes these condition types:
16 Advanced Programming Techniques (ELT 3296)
In practice, most scripts make extensive use of the [ or test command,
the shell’s Boolean check. On some systems, the [ and test commands are
synonymous, except that when the [ command is used, a trailing ] is also
used for readability. Note that you must put spaces between the [ braces
and the condition being checked.
Example: Checking whether a file exists using [ ] and test
if test -f myfile.txt if [ -f myfile.txt ]
then then
….. ……
fi fi
17 Advanced Programming Techniques (ELT 3296)
3.3.3. Arithmetic Expansion
Table below covers some common expression evaluations:
To evaluate an arithmetic expression, you can use expr command or
enclosing the expression by $((..))
Example:
X=0 X=0
X=$(( $x + 1)) X=$(expr $x +1)
3.3.4 Command execution
When you’re writing scripts, you often need to capture the result of a
command’s execution for use in the shell script; that is, you want to
execute a command and put the output of the command into a variable.
You can do this by using the $(command). The result of the $(command)
is simply the output from the command. Note that this isn’t the return
status of the command but the string output.
Example:
X=0
18 Advanced Programming Techniques (ELT 3296)
X=$(expr $x +1)
expr command is executed and stored result to variable x
Note: The $((…)) are used for arithmetic substitution. The $(…) is used for
executing commands and grabbing the output.
3.3.5 Control structures
The shell has a set of control structures, which are very similar to other
programming languages.
If
The if statement is very simple: It tests the result of a command and then
conditionally executes a group of statements:
Syntax Example
if condition
then
Statements
else
Statements
fi
For
Use the for construct to loop through a range of values, which can be any
set of strings.
Syntax Example
for variable in values
do
statements
done
The results in the following output:
bar fud 43
for var in {1..5}
do echo $var
19 Advanced Programming Techniques (ELT 3296)
done
While
When you need to repeat a sequence of commands, but don’t know in
advance how many times they should execute, you will normally use a
while loop.
Syntax Example
while condition do
statements
done
count=1
while [ $count -le 5 ]; do
echo $count
count=$((count+1))
done
Until
This is very similar to the while loop, but with the condition test
reversed. In other words, the loop continues until the condition becomes
true, not while the condition is true.
Syntax Example
until condition num=1
do until [ $num -ge 5 ]; do
Statements
echo $num
done
num=$((num+1))
20 Advanced Programming Techniques (ELT 3296)
done
Case
Case construct enables you to match the contents of a variable against
patterns and then allows execution of different statements, depending on
which pattern was matched
Syntax Example
case variables in
pattern [ | pattern] ….)
statements ;;
pattern [ | pattern] ….)
statements ;;
…
esac
You must be careful to put the most explicit matches first and the most
general match last. This is important because case executes the first
match it finds, not the best match.
3.3.6 Function
You can define functions in the shell; and if you write shell scripts of any
size, you’ll want to use them to structure your code. To define a shell
function, simply write its name followed by empty parentheses and
enclose the statements in braces.
When a function is invoked, the positional parameters to the script, $*,
$@, $#, $1, $2, and so on, are replaced by the parameters to the function.
That’s how you read the parameters passed to the function. When the
function finishes, they are restored to their previous values.
Syntax Example
21 Advanced Programming Techniques (ELT 3296)
function_name () { sum() {
statements echo $(( $1 + $2 ))
}
}
result=$(sum 5 3)
echo "Result: $result"
Typical output from this script might be as
follows:
4. Debugging and Optimizing Shell scripts
Debugging
22 Advanced Programming Techniques (ELT 3296)
bash -x script.sh
Optimizing
● Use functions to avoid repetition.
● Use built-in shell commands for efficiency.
5. Look up the syntax of command
On Linux, you can use the following commands to look up the syntax and
usage of a command or program:
● “man” command
man <command>
Example: man ls
● If you're unsure which category a command belongs to, search for
related topics:
man -k <keyword>
● “info” command (Some commands have an info page)
info <command>
● --help option (quick usage summary)
<command> --help
6. Additional Resources
● "The Linux Programming Interface" - Michael Kerrisk
● "The Linux Command Line" - William Shotts
II. Exercises
File and directory management shell command
Exercise 1: Execute following requests using shell command
1. Move to your user directory
23 Advanced Programming Techniques (ELT 3296)
2. Inside your user directory, create two subdirectories Slides, Labs
and a file name Solution.txt
3. Go to Labs directory
4. Inside Labs directory, create a subdirectory named Lab1 and a file
named Ex1.txt
5. Copy file Ex1.txt from labs directory to Lab1 directory.
6. Remove file Ex1.txt inside Labs directory
7. Rename of file Ex1.txt to Exercise1.txt
8. Add write mode access to file Exercise1.txt for other users
9. Remove directory Slides
Exercise 2: Continue exercise 1, go to Lab1 directory and try this
sequence of commands and record results of these commands.
1. cd
2. pwd
3. ls –al
4. cd .
5. pwd
6. cd ..
7. pwd
8. ls -al
9. cd ..
10. pwd
11. ls -al
Exercise 3: Create file Hello.txt inside Lab1 directory. Then using
echo command and redirecting to write below contents to file
Hello.txt.
Hello everyone!
This is Advanced Programming Techniques course.
24 Advanced Programming Techniques (ELT 3296)
There are many exercises for you in this course.
Let’s practice what we learn in this course with exercises.
Are you ready?
Process management shell command
Exercise 4: Use the I/O redirection and pipe mechanisms and Unix
commands to perform the following tasks:
1. Count number of word course appeared in file Hello.txt (you created
in exercise 3) and save results to file Ex4_results.txt.
2. Print 3rd line of the file Hello.txt, then count the number of words in
this line and save results to the end of file Ex4_results.txt.
(Hint: using sed, wc and pipe mechanism)
3. Store the names and attributels s of the directories and files inside
your user directory.
4. Count total the number of regular files and directories inside your
user directory and save it to the end of file Ex4_results.txt.
5. Count total the number of directories inside your user directory and
save it to the end of file Ex4_results.txt.
6. Count the total number of processes currently in the system and save
results to the end of file Ex4_results.txt
Shell script
Exercise 5: Write a set of commands in a shell script file named greeting.sh
that does the following:
a) Contains a comment with your name, the name of this script, and the
purpose of this script.
b) Displays the string “Hello + username”
c) Displays the name and version of this operating system.
d) Displays the group ID (GID) of the current user.
e) Displays the current date and time.
25 Advanced Programming Techniques (ELT 3296)
h) Displays a list of all the files in the current user’s directory.
i) Displays the values of the TERM, PATH, and HOME variables,
j) Displays “Goodbye + current time” to the user.
Exercise 6: Write a shell script named test.sh (with with any number of
parameters and parameter list) that displays the following information:
- Program name
- Number of command line parameters
- First command line parameter entered by the user
- All command line parameters entered by the user
Exercise 7: Write a shell script to convert a decimal integer to hexadecimal.
The shell script takes a command line parameter which is a decimal integer
to convert. Hint: using bc command to convert from decimal to
hexadecimal
Exercise 8: Program a shell script to print the Fibonacci sequence up to the
nth number. The Fibonacci sequence has the following characteristics:
- F(0) = 0
- F(1) = 1
- F(n) = F(n-1) + F(n-2) with n > 1
● Requirements:
- Enter the value n from the user.
- Print the Fibonacci sequence from F(0) to F(n).
26 Advanced Programming Techniques (ELT 3296)