Bash Scripting Notes
Bash Scripting Notes
Bash Scripting
Learn how to dance on terminal
Disclaimer
Dear readers,
This document is provided by VIEH Group for educational purposes only.
While we strive for accuracy and reliability, we make no warranties or
representations regarding the completeness, accuracy, or usefulness of the
information presented herein. Any reliance you place on this document is at
your own risk. VIEH Group shall not be liable for any damages arising from
the use of or reliance on this document. We acknowledge and appreciate the
contribution of the source person.
also,
This document is not created by a professional content writer so any mistake
and error is a part of great design
Scan QR:
1. OpeningvitoCreateaNewFile
○Openyourterminal.
vi my_script.sh
○ Note: If my_script.sh doesn’t exist, vi will create a new file with
that name.
2. BasicviEditingCommands
When you open vi, you’re in command mode, where you can issue
commands to vi. Here’s how to enter insert mode to start writing:
○
Pressitoenterinsertmode.Youcannowtypecontentintothefile.
3. WritingContentinvi
Let’s create a simple script that prints "Hello, World!" and displays the
current date and time. With vi in insert mode, type the following script:
#!/bin/bash
date
4. ExitInsertMode:
Press Esc to return to command mode.
5. SavingandExitinginvi
To save the file and exit vi:
○Type:wqandpressEnter.
:wq means "write (save) and quit."
Alternatively:
○ Tosavewithoutexiting,type:wandpressEnter.
○ Toquitwithoutsavingchanges,type:q!andpressEnter.
6. MakingtheScriptExecutable
Before running your script, you need to make it executable. In the
terminal, type:
chmod +x my_script.sh
7. RunningtheScriptandViewingOutput
Run the script by typing:
./my_script.sh
8. RuntheScriptWithoutMakingItExecutable:
You can directly run it with bash by typing:
bash my_script.sh
Multi-Line Comments:
Bash does not have a direct syntax for multi-line comments, but you can
simulate it using a here-document with <<.
: <<'COMMENT'
COMMENT
<<comment
It won’t be executed.
comment
Output:
Hello World!
WhyShebang( #!/bin/bash)Matters
This line tells the system to use Bash to run the script, ensuring compatibility
across Linux environments.
echo "${myArray[1]}"
Outputs: 2
declare -A myArray
Access Values:
echo "${myArray[name]}"
Update Array:
myArray+=( [city]=NewYork )
Length:
echo ${#str}
Replace: Outputs: 15
echo ${str/Scripting/Programming} Outputs: Shell Programming
Extract Substring:
echo ${str:6:9}
Outputs: Scripting
Convert to uppercase:
upper=${myVar^^}
echo$upper Output:HELLOWORLD!
Convert to lowercase:
lower=${myVar,,}
echo$lower Output:helloworld!
Replace a substring:
replace=${myVar/World/Buddy}
echo $replace Output: Hello Buddy!
● BasicInput:
read var_name
echo "You entered: $var_name"
Example Output:
(User types "John")
You entered: John
Key Difference:
● BasicInput:Takesinputwithoutaprompt.
● Input with Prompt: Displays a prompt to guide the user.
🔹 Arithmetic Operations
● Usingtheletcommand:
Increment:
let a++
This increments the value of a by1.
Increment:
((a++))
Key Difference:
● letismoretraditional,while(( ))ismoremodernandallowsformore
complex arithmetic expressions.
if [ $a -gt $b ]; then
echo "a is greater than b"
fi
else
fi
else
fi
Case Statement:
case $a in
2" ;;
esac
● Alwaysputspacesaroundoperatorsinconditions.
● elif and else are optional but useful for handling multiple conditions.
Comparison Operators
Greater Than or Equal to: -ge: Checks if the left operand is greater than or
equal to the right.
[ $a -ge $b ]
Less Than or Equal to: -le: Checks if the left operand is less than or equal to
the right.
[ $a -le $b ]
Not Equal to: -ne or !=: Checks if two values are not equal.
[ $a -ne $b ]
Greater Than: -gt : Checks if the left operand is greater than the right.
[ $a -gt $b ]
Less Than: -lt : Checks if the left operand is less than the right.
[ $a -lt $b ]
Explanation:
● The&&ensuresbothconditionsmustbetrue.
● The||checksthesecondconditionifthefirstfails.
a=10
[ $a -gt 5 ] && echo "Greater" || echo "Not Greater"
Explanation:
🔄 For Loop
The for loop iterates over a list or a range of values and performs actions for
each item.
Syntax:
Example:
foriin123;do
echo "Number: $i"
done
Output:
Number: 1
Number: 2
Number: 3
Output:
Count: 1
Count: 2
Count: 3
🔄 While Loop
The while loop runs as long as the specified condition is true.
Syntax:
while [ condition ]; do
# Commands to execute
done
Example:
count=1
while [ $count -le 3 ]; do
echo "Count is: $count"
((count++)) # Increment count
done
Output:
Count is: 1
Count is: 2
Count is: 3
Syntax:
until [ condition ]; do
# Commands to execute
done
Example:
count=1
until [ $count -gt 3 ]; do
echo "Count is: $count"
((count++))
done
Output:
Count is: 1
Count is: 2
Count is: 3
🔄 Infinite Loop
An infinite loop continues running indefinitely until it is manually stopped (e.g.,
using Ctrl+C).
For Loop Infinite Example:
for (( ; ; )); do
done
done
done
...
🖥 Select Loop
The select loop creates a simple menu system, which allows users to select an
option from a list. It's useful when you need a user-driven selection process.
Syntax:
Example Output:
1) Apple 2)
Banana 3) Orange
4) Exit Choose a
fruit: 2 You
chose Banana
Explanation:
●
PS3setsthepromptmessage.
● Theselectloopdisplaysoptions,andeachselectionrunsthe
corresponding case statement.
● Thebreakstatementexitstheloopwhentheuserselects"Exit."
🔄 Functions
1. DefiningFunctions:
2. BasicFunction:
greet() {
echo "Hello, welcome to the shell script!"
}
greet # Calling the function
greet_user() {
echo "Hello, $1!"
}
greet_user "Adhyansh"
Functions return values via echo, and the output can be captured.
add_numbers() {
result=$(( $1 + $2 ))
echo $result
}
sum=$(add_numbers 3 5)
echo "The sum is: $sum"
check_even() {
if (( $1 % 2 == 0 )); then
echo "$1 is even"
else
echo "$1 is odd"
fi
}
check_even 7 output: "7 is odd"
6. Recursion:
Functions can call themselves recursively.
factorial() {
if [ $1 -le 1 ]; then
echo 1
else
prev=$(factorial $(( $1 - 1 )))
echo $(( $1 * prev ))
fi
}
7. DefaultValues:
greet() {
local name=${1:-Guest}
echo "Hello, $name!"
}
greet"Adhyansh" Output:"Hello,Adhyansh!"
greet Output:"Hello,Guest!"
8. PassingArgumentsbyReference:
Bash doesn't directly support passing by reference but can simulate it using
eval.
modify_value() {
eval $1=\$2
}
modify_value var 100
echo "The value of var is now: $var" Output: "The
value of var is now: 100"
9. ArgumentPassing:
You can pass arguments to functions, and they can be accessed inside the
function.
■ PositionalArguments:$1,$2,$3,etc.(Accessindividual
arguments).
● Theserepresentindividualargumentspassedtoafunctionorscript.
● $1isthefirstargument,$2isthesecond,andsoon.
Example:
greet() {
echo "Hello, $1! You are $2 years old."
}
greet "Adhyansh" 25
● Thisrepresentsalltheargumentspassedtoafunctionorscript.
● Eachargumentistreatedasaseparateword,whichisespeciallyuseful
when looping over the arguments.
Example:
print_all() {
for arg in "$@"; do
echo "$arg"
done
}
print_all "Apple" "Banana" "Cherry"
Output:
Apple
Banana
Cherry
● Similarto$@,butittreatsallargumentsasasinglestring,meaningspaces
between arguments may be lost.
Example:
print_all_as_string() {
echo "$*"
}
print_all_as_string "Apple" "Banana" "Cherry"
Output: Apple Banana Cherry
● Thisgivesthecountoftheargumentspassedtoafunctionorscript.
Example:
count_args() {
echo "Number of arguments: $#"
}
count_args "Apple" "Banana" "Cherry"
Output: Number of arguments: 3
Summary
Syntax:
● shift:Shiftspositionalparametersleftbyoneposition.
● shift n:Shiftspositionalparametersleftbynpositions.
shift_example() {
echo "Original arguments: $1, $2, $3"
shift
echo "After shift: $1, $2, $3"
}
shift_example "one" "two" "three"
Output:
Original arguments: one, two, three
After shift: two, three
After the shift, $1 becomes "two", and $2 becomes "three".
Output:
Processing argument: arg1 Processing argument: arg2
Processing argument: arg3 Processing argument: arg4
The loop processes each argument one by one by shifting the arguments left until
there are no more arguments ($# becomes 0).
After shifting by 2, the first two arguments are removed, and the remaining
arguments are accessible using $@.
Key Points:
● shift removes the first argument ($1) and shifts the remaining arguments
left.
● You can shift by multiple positions using shift n.
● The $# variable always reflects the remaining number of arguments.
● The shift command is useful in loops for processing a variable number of
arguments.
break Statement
● Purpose:Exitsaloopprematurely.
● Syntax:breakorbreak n(exitstheloopandcanexitmultiplenested
loops with n).
Example:
for i in {1..5}; do
if [ $i -eq 3 ]; then break; fi
echo $i
done
● Output:Loopsuntili=3,thenexits.
● Purpose:Skipsthecurrentloopiterationandmovestothenextone.
● Syntax:continueorcontinue n(skipscurrentiterationinnestedloops).
Example:
for i in {1..5}; do
if [ $i -eq 3 ]; then continue; fi
echo $i
done
● Output:Skipsiterationwhen i=3.
Example:
for i in {1..3}; do
for j in {1..3}; do
if [ $i -eq 2 ] && [ $j -eq 2 ]; then break 2; fi
echo "i=$i, j=$j"
done
done
● Output:Exitsbothloopswheni=2andj=2.
4. sleep Command
● Purpose:Pausesthescriptforaspecifiedtime.
● Syntax:sleep <duration>
Example:
● Purpose:Exitsascriptwithastatuscode(0forsuccess,non-zeroforerror).
● Syntax:exit <exit_code>
Example:
Example:
mkdir myfolder
Summary
● break:Exitloops.
● continue:Skipsaniterationinloops.
● sleep:Pausesscriptexecution.
● exit:Terminatesascriptwithastatuscode.
● $?:Checkstheexitstatusofthelastcommand.
seq command
● start:Thestartingnumberofthesequence.
● end:Theendingnumberofthesequence.
seq 1 5
Output:
1 2 3 4 5
Custom Step Size: You can specify a step size between numbers. For example, to
generate numbers from 1 to 10 with a step size of 2:
seq 1 2 10
Output:
1
3
5
7
9
Generating a Sequence with Decimal Values: You can also use floating-point
numbers with seq. For example, generate numbers from 0 to 1 with a step size of
0.2:
seq 0 0.2 1
Using seq with for Loop: You can use seq ina for loop. For example, printing
numbers from 1 to 5:
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Output:
Number: 1.00 Number: 2.00 Number: 3.00 Number: 4.00 Number:
5.00
-s: Change the separator between numbers. By default, it’s a newline, but you can
change it:
seq -s "," 1 5
Output:
1,2,3,4,5
-w: Pad numbers with leading zeros to make them all the same width:
seq -w 1 5
Output:
01 02 03 04 05
Summary:
Basic Syntax:
{item1,item2,item3}
This will generate a list containing the individual items within the braces.
Output:
apple banana cherry
echo {1..5}
Output:
1 2 3 4 5
Range of Letters: You can also create a range of letters:
echo {a..e}
Output:
a b c d e
Output:
1 3 5 7 9
Combining Text with Variables: You can use brace expansion to combine static
text with variables:
echo file{1..3}.txt
Output:
file1.txt file2.txt file3.txt
Nested Brace Expansion: You can also nest brace expansions to create more
complex patterns:
echo {A,B}{1,2}
Output:
A1 A2 B1 B2
Multiple Elements in a Single Expansion: You can combine multiple sets of values
within a single brace expansion:
echo {A,B,C}{1,2,3}
Output:
A1 A2 A3 B1 B2 B3 C1 C2 C3
Important Points:
● Brace expansion happens before any other shell operation like variable
expansion or command substitution.
● It is not the same as parameter expansion or globbing. It is handled
directly by the shell before the command is executed.
● Nospacesbetweencommasinthebraces.Itshouldbe{item1,item2}
insteadof{item1, item2}.
Summary:
Brace expansion is a powerful feature in the shell that allows you to generate
sequences of strings, numbers, or characters without writing them out explicitly. It
can save time and reduce errors when generating repetitive patterns, filenames,
or arguments.
getopts command
Basic Syntax:
● getoptsprocessescommand-lineargumentsonebyone.
● Itsetsthevalueofthevariabletothecurrentoptionletter.
● Iftheoptionrequiresanargument,getoptswillassignthatargumenttoa
special variable, usually $OPTARG.
#!/bin/bash
while getopts "ab" option; do
case $option in
a) echo "Option A selected" ;; b)
echo "Option B selected" ;; \?) echo
"Invalid option"; exit 1 ;;
esac
done
Running the script:
$ ./myscript.sh -a
Option A selected
$ ./myscript.sh -b
Option B selected
You can also specify options that require arguments (e.g., -f filename).
#!/bin/bash
while getopts "f:n:" option; do
case $option in
f) echo "Option F selected with argument: $OPTARG" ;;
n) echo "Option N selected with argument: $OPTARG" ;;
\?) echo "Invalid option"; exit 1 ;;
esac
done
$ ./myscript.sh -n 123
Option N selected with argument: 123
For options that have both short and long forms, you can use getopts to handle
short options (e.g., -f) but long options require additional parsing logic. Here's an
example for a simple long-option-like approach.
● $OPTARG:Thisvariablecontainsthevalueoftheargumentpassedtoan
option (if any).
● $option:Thecurrentoptionbeingprocessed.
● \?: Used in the case statement to catch invalid options.
ScriptExample( myscript.sh):
#!/bin/bash
while getopts "a:b:c:" option; do
case $option in
a) echo "Option A selected with value: $OPTARG" ;;
b) echo "Option B selected with value: $OPTARG" ;;
c) echo "Option C selected with value: $OPTARG" ;;
\?) echo "Invalid option"; exit 1 ;;
esac
done
#!/bin/bash
if [ $# -eq 0 ]; then
exit 1
fi
case $option in
esac
done
Key Points:
● Providesasimpleandstandardwaytohandlecommand-lineoptions.
● Makesscriptsmoreuser-friendlyandinteractive.
1. basename:
● Stripsthedirectorypathandreturnsonlythefilename.Youcanalsoremove
a file extension if specified.
● Example:basename /home/user/file.txt → file.txt,and
basename /home/user/file.txt .txt → file.
2. dirname:
● Stripsthefilenameandreturnsthedirectorypath.
● Example:dirname /home/user/file.txt →/home/user.
3. realpath:
● Resolvesandreturnstheabsolutepathofafileordirectory,resolving
symbolic links.
● Example:realpath file.txt →
/home/user/Documents/file.txt.
● -d <folder>:Checksifadirectoryexists.
● ! -d <folder>:Checksifadirectorydoesnotexist.
● -f <file>:Checksifafileexists.
● ! -f <file>:Checksifafiledoesnotexist.
Examples:
if [ -d /home/user/Documents ]; then echo "Directory
exists."; fi
● RANDOM:Generatesarandomnumberbetween0and32767.
● UID:StorestheuserIDofthecurrentlylogged-inuser.
Examples:
#Generatesarandomnumber
echo$RANDOM
echo $((RANDOM % 101)) # Random number between 0 and 100
echo$UID #UserIDofthelogged-inuser
These commands are essential tools for file path manipulation, file existence
checking, and working with system information in Bash scripts. Let me know if
you'd like further clarification or examples!
To continue running even after the terminal is closed, you can use nohup (short
for "no hang up"). This command is useful for running long-running processes or
scripts in the background, even if the terminal session is closed or disconnected.
Example:
nohup bash myscript.sh &
This will run myscript.sh in the background, and the terminal can be closed
without interrupting the execution of the script. The standard output will be
redirected to a file called nohup.out unless you specify a different file.
❖ Redirecting Output:
If you don't want to see the output in the nohup.out file, you can redirect it
to /dev/null.
Alternatively, you can bring the background job to the foreground using fg
andthenstopitwith Ctrl + C:
fg%1 # Bring job 1 to the foreground
Summary:
● nohup is used to run a command or script that will continue executing even
after the terminal is closed.
● The & operator runs the command in the background.
● You can redirect output to a file or /dev/null to suppress output.
● Use jobs and ps to check the status of background jobs.
Steps:
1. Createanewfileforthescript:Openaterminalandcreateanewfile
named calculator.sh using a text editor.
vi calculator.sh
#!/bin/bash
# Function to add two numbers
add() {
echo "Result: $(($1 + $2))"
}
case $choice in
1)
add $num1 $num2
;;
2)
subtract $num1 $num2
;;
3)
multiply $num1 $num2
;;
4)
divide $num1 $num2
;;
*)
echo "Invalid choice"
;;
esac
4. Run the script: Now you can run the calculator script using:
./calculator.sh
Example Output:
$ ./calculator.sh Simple
Calculator Choose
operation: 1. Add 2.
Subtract 3. Multiply 4.
number: 10
Result: 15
This small project helps you understand basic conditional statements, user input
handling, and mathematical operations in Bash. You can expand this project by
adding more advanced operations (like square roots, exponents, etc.) or even a
loop to make the calculator run repeatedly.
**************************************************************************************
Jai hind