Bash NVV Nverseen
Bash NVV Nverseen
By: Jaswanth T
___
Notes
echo $0
3. Writing Content in vi
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
Single-Line Comments:
Use the # symbol at the beginning of a line to create a single-line comment.
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
It won’t be executed.
comment
"Hello World!"
Output:
Hello World!
Make it Executable:
chmod +x script.sh
NAME="Linux"
readonly VERSION="1.0"
echo "${myArray[1]}"
Outputs: 2
This fetches all elements starting from the second element (index 1).
● To get a specific range of values: echo
"${myArray[*]:1:2}"
myArray+=(5 6 8)
Length:
Replace:
echo ${str/Scripting/Programming} Outputs: Shell Programming
Extract Substring:
echo ${str:6:9} Outputs: Scripting
Replace a substring:
replace=${myVar/World/Buddy} echo
$replace Output: Hello Buddy!
Example Output:
(Your name: User types "John")
Your name: John
Hello, John!
Key Difference:
🔹 Arithmetic Operations
Increment:
((a++))
Key Difference:
Statement:
if-else Statement:
if [ $a -gt $b ]; then
b" fi
else
fi
Case Statement:
case $a in
esac
Key Notes:
Comparison Operators
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 ]
🔹 Logical Operators
Using && (AND) Operator:
a=10
b=5
if [ $a -gt 5 ] && [ $b -lt 10 ]; then
echo "Both conditions are true"
else echo "One or both conditions are
false"
fi
Explanation:
a=10
[ $a -gt 5 ] && echo "Greater" || echo "Not Greater"
Explanation:
The for loop iterates over a list or a range of values and performs actions for
each item.
Syntax:
for item in list; do
# Commands to execute for each item
done
Example:
for i in 1 2 3; do
echo "Number: $i"
done
Output:
Number: 1
Number: 2
Number: 3
Range Example:
for i in {1..3}; do
echo "Count: $i"
done
Output:
Count: 1
Count: 2
Count: 3
🔄 While Loop
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
🔄 Until Loop
The until loop continues to execute until the condition becomes true.
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
for (( ; ; )); do
done
while :; do
done
until false; do
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:
select option in list; do
# Commands based on user choice
done
Example:
PS3="Choose a fruit: " select fruit in Apple
Banana Orange Exit; do
case $fruit in
Apple) echo "You chose Apple";;
Banana) echo "You chose Banana";;
Orange) echo "You chose Orange";;
Exit) break;;
*) echo "Invalid option";;
esac
done
Example Output:
1) Apple
2) Banana
3) Orange
4) Exit
Choose a fruit: 2
You chose Banana
Explanation:
● PS3 sets the prompt message.
● The select loop displays options, and each selection runs the
corresponding case statement.
● The break statement exits the loop when the user selects "Exit."
💡 Tip:
Loops are powerful tools for automating repetitive tasks. You can use them for
various purposes like iterating over files, arrays, or ranges. For example, you can
rename all files in a directory using a loop.
🔄 Functions
1. Defining Functions: You can define a function using either of these two
syntaxes:
code.
greet() {
echo "Hello, welcome to the shell script!"
}
greet # Calling the function
greet_user() {
echo "Hello, $1!"
}
greet_user "Adhyansh"
4. Return Values: 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"
loops.
check_even() {
if (( $1 % 2 == 0 ));
then echo "$1 is even"
else echo "$1 is odd" fi
}
check_even 7 output: "7 is odd"
7. Default Values:
greet() {
local name=${1:-Guest}
echo "Hello, $name!"
}
greet "Adhyansh" Output: "Hello, Adhyansh!" greet
Output: "Hello, Guest!"
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. Argument Passing:
You can pass arguments to functions, and they can be accessed inside the
function.
■ Positional Arguments: $1, $2, $3, etc. (Access individual
arguments).
■ All Arguments: $@ (all arguments as separate words), $* (all
arguments as a single string).
■ Argument Count: $# (number of arguments passed).
greet() {
echo "Hello, $1! You are $2 years old."
} greet "Adhyansh"
25
print_all_as_string() {
echo "$*" } print_all_as_string "Apple"
"Banana" "Cherry" Output: Apple Banana Cherry
Example:
count_args() {
echo "Number of arguments: $#"
} count_args "Apple" "Banana"
"Cherry" Output: Number of
arguments: 3
Summary
The shift command in Bash allows you to move (shift) the positional parameters
(arguments) passed to a script or function. Here's a brief explanation of how it works:
Syntax:
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
for i in {1..5}; do
if [ $i -eq 3 ]; then break; fi
echo $i
done
● Purpose: Skips the current loop iteration and moves to the next one.
● Syntax: continue or continue n (skips current iteration in nested
loops). Example:
for i in {1..5}; do
if [ $i -eq 3 ]; then continue; fi
echo $i
done
● Output: Skips iteration when i=3.
loops. 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: Exits both loops when i=2 and j=2.
4. sleep Command
● Purpose: Exits a script with a status code (0 for success, non-zero for
error).
● Syntax: exit <exit_code> Example:
● Purpose: Stores the exit status of the last executed command. Example:
mkdir myfolder echo $? # Returns 0 if
successful, 1 if failed.
Summary
seq command
The seq command in Linux is used to generate a sequence of numbers. It's commonly
used in shell scripting or on the command line to generate numbers for loops, lists, or
ranges.
Basic Syntax:
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
Output:
0.0
0.2
0.4
0.6
0.8
1.0
Reverse Sequence: You can reverse the order of the sequence by making the start
number greater than the end number: seq 5 -1 1
Output:
5
4
3
2
1
Using seq with for Loop: You can use seq in a for loop. For example, printing
numbers from 1 to 5:
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Common Options with seq:
-f: Format the output using a format string (like printf). seq
-f "Number: %.2f" 1 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:
The seq command is a powerful and simple tool to generate sequences of numbers,
and it can be customized in many ways using options for formatting, step size, and
separators.
Brace Expansion is a feature in the shell (especially Bash) that allows you to generate
multiple text strings from a single expression. It is typically used to create lists of strings
or numbers, or to generate filenames with multiple variations. Brace expansion is a
simple and efficient way to generate multiple values without having to manually list
them.
Basic Syntax:
{item1,item2,item3}
This will generate a list containing the individual items within the braces.
Creating a List of Words: You can generate a list of words by expanding a comma-
separated list inside braces:
echo {apple,banana,cherry}
Output:
apple banana cherry
Range of Numbers: You can create a sequence of numbers by using a range inside the
braces:
echo {1..5}
Output:
1 2 3 4 5
Output:
a b c d e
Range with Step Size: You can specify a step size by including it after the range:
echo {1..10..2}
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
Files and Directories: Brace expansion can also be used to generate multiple file or
directory names:
mkdir dir{1,2,3}
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.
● No spaces between commas in the braces. It should be {item1,item2}
instead of {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:
This example shows how to handle single-letter options, such as -a or -b, in a script.
#!/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
Example 2: Options with Arguments
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 -f file.txt
Option F selected with argument: file.txt
$ ./myscript.sh -n 123
Option N selected with argument: 123
#!/bin/bash
while getopts "f:n:" option; do
case $option in
f) echo "File option: $OPTARG" ;;
n) echo "Name option: $OPTARG" ;;
\?) echo "Invalid option"; exit 1 ;;
esac
done
● $OPTARG: This variable contains the value of the argument passed to an option
(if any).
● $option: The current option being processed.
● \?: Used in the case statement to catch invalid options.
If no arguments are given or the getopts finds a missing or incorrect argument, it will
return ?, which can be caught to display an error or usage message.
Example: #!/bin/bash
if [ $# -eq 0 ]; then
exit 1
fi
case $option in
esac
done
Key Points:
Advantages:
1. basename:
● Strips the directory path and returns only the filename. You can also remove a file
extension if specified.
● Example: basename /home/user/file.txt → file.txt, and
basename /home/user/file.txt .txt → file.
2. dirname:
● Resolves and returns the absolute path of a file or directory, resolving symbolic
links.
● Example: realpath file.txt →
/home/user/Documents/file.txt.
4. File/Directory Existence Checks:
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. ❖ How to Use
nohup:
To run a script in the background and keep it running after closing the terminal,
use the nohup command followed by the script or command and an & to run
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.
You can check if the script is running by using the ps command or jobs
command.
ps aux | grep myscript.sh
Or:
jobs
❖ Stopping Background Jobs:
To stop a background job, you can use the kill command with the process ID
(PID) of the job.
kill <PID>
Alternatively, you can bring the background job to the foreground using fg and
then stop it with 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. Create a new file for the script: Open a terminal and create a new file named
calculator.sh using a text editor.
vi calculator.sh
2. Add the script code: Below is the code for the calculator:
#!/bin/bash
# Function to add two numbers
add() { echo "Result: $(($1 +
$2))"
}
case $choice in
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. Divide
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.
**********************************************************************
****************