Introduction
- Shell scripting is an essential skill for anyone working in Unix or Linux environments.
- A shell script is a program written for the shell, or command-line interpreter, of an operating system.
- It automates repetitive tasks and can combine various commands to execute complex processes.
- There are different types of shell scripting, variable types, operators, and data structures that can be leveraged to write effective scripts.
Types of Shell Scripting
Bash (Bourne Again Shell)
:- One of the most common shell types used in Linux environments.
- It’s an extension of the Bourne shell and provides features like command-line editing and job control.
Sh (Bourne Shell)
:- The original Unix shell developed by Stephen Bourne, and it serves as the foundation for many other shells like Bash and Korn shell.
Zsh (Z Shell)
:- A powerful shell similar to Bash but with additional features such as built-in spell checking and improved scripting capabilities.
Csh (C Shell)
:- The C shell’s syntax is similar to the C programming language, and it provides features like command history and job control.
Ksh (Korn Shell)
: -A high-performance shell developed by David Korn, it blends features of the Bourne and C shells.
Variables
- Variables in shell scripts are containers for storing data, and they do not require a specific data type declaration(default - string).
1
2
3
| #!/bin/bash
name="Scripting"
echo "The topic is $name"
|
Operators
- Operators allow you to perform operations like arithmetic, comparisons, assignments, and more. Shell scripting supports a wide variety of operators.
- Arithmetic Operators
- Arithmetic operations can be done using the expr command or $((…)) syntax.
1
2
3
4
5
| #!/bin/bash
a=10
b=5
sum=$((a + b))
echo "Sum: $sum"
|
- Common arithmetic operators:
- Comparison Operators
- Comparison operators are used to compare values, typically integers, in a script. These comparisons are often used in if statements.
1
2
3
4
5
6
7
8
9
| #!/bin/bash
a=10
b=20
if [ $a -eq $b ]; then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
|
- Common comparison operators:
- -eq (Equal to)
- -ne (Not equal to)
- -gt (Greater than)
- -lt (Less than)
- -ge (Greater than or equal to)
- -le (Less than or equal to)
- Assignment Operators
- Assignment operators assign values to variables.
1
2
3
4
5
| #!/bin/bash
a=10
a+=5 # Equivalent to a = a + 5
echo "a: $a"
Operators like +=, -=, *=, /=, and %= perform the respective operations and then assign the result to the variable.
|
- Logical Operators
- Logical operators are used to combine conditions.
1
2
3
4
5
6
7
| #!/bin/bash
a=10
b=20
if [[ $a -lt 15 && $b -gt 15 ]]; then
echo "Both conditions are true"
fi
|
- Common logical operators:
- && (Logical AND)
- ! (Logical NOT)
- Bitwise Operators
- Bitwise operators perform bit-level operations.
1
2
3
4
5
| #!/bin/bash
a=5 # Binary: 101
b=3 # Binary: 011
result=$((a & b)) # Bitwise AND
echo "Result of AND: $result"
|
- Bitwise operators:
- & (AND) | (OR) ^ (XOR) « (Left shift)
(Right shift)
- Identity Operators Identity operators are used to compare whether two objects refer to the same object.
bash
#!/bin/bash a=”hello” b=”hello”
if [ “$a” == “$b” ]; then echo “a and b are identical” fi