Shell Scripting – SEBI Grade A IT (Exam
Notes)
1. Introduction
• A shell script is a text file containing a sequence of Linux commands with logic
(variables, loops, conditions).
• Common shell: bash.
Shebang (first line of script):
#!/bin/bash
This tells the system which interpreter to use.
Steps to run a script:
chmod +x [Link] # give execute permission
./[Link] # run
# or
bash [Link]
2. Command-Line Arguments & Special Variables
When you run:
./[Link] arg1 arg2 arg3
Inside [Link] the shell provides special variables:
PYQ: “Which special variable in Linux shell scripting gives the number of parameters used
to script?”
Answer: $#
TVK – Telegram, YouTube
Example 1 – Simple greeting
#!/bin/bash
echo "Hello: $1"
Run:
./[Link] Alisha
Output:
Hello: Alisha
3. Conditions, Comparisons & exit Status
3.1 Numeric comparisons in [ ]
Used for comparing numbers:
• -lt → less than
• -le → less than or equal
• -gt → greater than
• -ge → greater than or equal
• -eq → equal
• -ne → not equal
Important: There must be spaces:
if [ "$age" -le 0 ]; then
echo "Wrong value"
fi
(Not: [ $age-le 0 ])
3.2 Exit status of commands – $?
• Every command returns an exit status between 0 and 255.
• 0 → success
• Non-zero → error
Check using $?:
pwd -adsfffsf # invalid option
echo $? # non-zero (command failed)
3.3 exit command in scripts
exit N ends the script immediately and sets exit status to N.
TVK – Telegram, YouTube
• If N is not given, exit code of the last command is used.
• This can be used by other scripts/commands.
Example – age validation:
#!/bin/bash
age="$1"
if [ "$age" -le 0 ]; then
echo "You seem to have entered wrong value"
echo "Age cannot be negative or zero"
exit 1 # non-zero → failure
fi
echo "Welcome to shell programming! Let's get started"
If you run:
./[Link] -5
Output:
You seem to have entered wrong value
Age cannot be negative or zero
Script stops at exit 1.
4. Variables in Shell Scripting
4.1 Normal shell variables
• No data types; everything is string by default.
• No spaces around =.
name="Sanjay" # correct
age=25 # correct
x = 10 # incorrect
Use them with $:
echo "Name is $name and age is $age"
4.2 Environment variables
Environment variables are visible to child processes (sub-shells, programs).
Some common ones:
• PATH, HOME, USER, SHELL etc.
TVK – Telegram, YouTube
Set environment variable:
MYVAR="hello"
export MYVAR
# or simply
export MYVAR="hello"
Check:
echo "$MYVAR"
PYQ type: “How to set environment variables in shell?”
✔ Use: export VAR=value
5. return vs exit
Both give a status code (0–255), but they behave differently:
• exit N
o Used in main script.
o Terminates the entire script.
• return N
o Used inside functions.
o Returns from the function to the caller but continues script execution.
Example: function with return
#!/bin/bash
check_even() {
if [ $(( $1 % 2 )) -eq 0 ]; then
return 0 # even
else
return 1 # odd
fi
}
check_even "$1" # call function with 1st argument
if [ $? -eq 0 ]; then
echo "Even number"
else
echo "Odd number"
fi
TVK – Telegram, YouTube
6. Important Basic Linux Commands (with Examples)
These are high-yield for objective questions and practical understanding.
6.1 Date, calendar, user info
Command Description Example
date Show current date & time date
cal Show calendar cal, cal 2025
whoami Show current logged-in user whoami
6.2 Directory & path commands
Command Description Example
pwd Print current working directory path pwd → /home/sanjay/projects
cd Change directory cd /var/log, cd .., cd ~
mkdir Make a new directory mkdir notes, mkdir -p a/b/c
PYQ: “Which Linux command is used to display the current directory path?”
Answer: pwd
(cwd is only a term for current working directory, not a standard command.)
6.3 File-related commands
Command Description Example
ls List files/directories ls, ls -l, ls -a, ls *.sh
file Show file type file [Link]
touch Create empty file/update timestamp touch [Link]
cp Copy files/directories cp [Link] [Link], cp -r dir1 dir2
mv Move/rename files & directories mv [Link] [Link], mv file /tmp/
rm Remove files/directories rm [Link], rm -r dir, rm -rf dir
clear Clear terminal screen clear or Ctrl+L
open is mainly a macOS command:
• macOS: open [Link] → opens file in default app.
• On Linux: equivalent is usually xdg-open [Link].
6.4 Wildcards, echo, tail, sort
TVK – Telegram, YouTube
Topic /
Description Example
Command
*
Wildcard: matches any sequence of ls *.sh → all files ending with
characters .sh
echo "Hello", echo "User:
echo Print text/variables $USER"
tail [Link], tail -n 20
tail Show last few lines of a file [Link]
sort [Link], sort -r, sort
sort Sort lines of text -n
You had tall in your list – that’s almost certainly tail.
6.5 Permissions & ownership
Command Description Example
chmod +x [Link], chmod 644
chmod Change file permissions [Link]
chown Change file owner sudo chown sanjay [Link]
chgrp Change file group sudo chgrp developers [Link]
sudo
Run a command as superuser sudo apt update
(root)
Numeric permissions:
• r= 4, w = 2, x = 1
• Example: chmod 755 [Link]
o Owner: 7 → rwx
o Group: 5 → r-x
o Others: 5 → r-x
6.6 Processes & prioritization
Command Description Example
ps Show running processes ps, ps aux
kill Send signal to a process kill 1234
killall Kill all processes with a given name killall firefox
nice Start process with a specified priority (niceness) nice -n 10 [Link]
renice Change priority of an already running process renice +5 -p 1234
PYQ: “Which Linux command is used to change the priority of a running process?”
Answer: renice
TVK – Telegram, YouTube
6.7 Searching, piping, redirecting
Pipe (|) – connects output of one command to input of another:
ls -l | grep ".sh"
Redirection:
Symbol Meaning Example
> Redirect stdout (overwrite file) ls > [Link]
>> Redirect stdout (append to file) echo "log" >> [Link]
2> Redirect stderr command 2> [Link]
2>&1 Send stderr to same place as stdout command > [Link] 2>&1
grep – search text:
grep "error" [Link]
grep -i "error" [Link] # case-insensitive
grep -r "main" src/ # recursive in directory
find – locate files/directories:
find . -name "*.log"
find /etc -type f -name "hosts"
which – show full path of command:
which bash
which python
7. Loops in Shell Scripting
7.1 for loop – over a list
for name in Alice Bob Charlie
do
echo "Hello $name"
done
7.2 for loop – C-style (bash specific)
for (( i=1; i<=5; i++ ))
do
echo "$i"
done
7.3 while loop
TVK – Telegram, YouTube
Used when we don’t know the number of iterations beforehand.
count=1
while [ "$count" -le 5 ]
do
echo "Count = $count"
count=$((count + 1))
done
7.4 until loop
Runs until condition becomes true (opposite of while).
count=1
until [ "$count" -gt 5 ]
do
echo "Count = $count"
count=$((count + 1))
done
7.5 break and continue
for (( i=1; i<=10; i++ ))
do
if [ "$i" -eq 5 ]; then
continue # skip 5
fi
if [ "$i" -gt 8 ]; then
break # stop loop
fi
echo "$i"
done
8. PYQ Corner – Direct Answers
1. Change priority of a running process → renice
2. Special variable for number of parameters in shell script → $#
3. Set environment variable in shell → export VAR=value
4. Display current directory path → pwd
TVK – Telegram, YouTube
9. Ultra-Short Revision Page (for last-minute glance)
• Script basics: #!/bin/bash, chmod +x file, ./file
• Arguments:
o $0 – script name
o $1..$9 – arguments
o $# – number of arguments
o $*, $@ – all arguments
• Exit status:
o $? – last command’s status
o 0 – success; non-zero – failure
o exit N – exit script
o return N – return from function
• Variables:
o Normal: name="Sanjay"
o Environment: export JAVA_HOME=/path
• Core commands (one-liners):
o pwd – current directory
o cd – change directory
o ls – list files
o mkdir – make directory
o cp, mv, rm – copy, move, remove
o chmod, chown, chgrp, sudo – permissions/ownership
o kill, killall, nice, renice – processes & priority
o grep, find, which – searching
o |, >, >>, 2> – pipeline & redirection
• Loops:
o for, while, until, break, continue
TVK – Telegram, YouTube