Bash (Bourne Again Shell) is a command-line interpreter used in Linux and Unix operating systems. It allows users to interact with the system by running commands and programs. Bash Shell Scripting is the process of writing scripts in Bash to automate repetitive tasks, perform file operations, and manage system activities more efficiently.
Simple Bash Script
#!/bin/bash
echo "Welcome to Bash Scripting"
echo "Enter your name:"
read name
echo "Hello, $name!"
echo "This is a simple Bash script."
Key Features of Bash Shell Scripting
1. Compatibility
Bash is compatible with the original UNIX Bourne shell and also includes useful features from Korn and C Shell.
Because of this, many shell scripts can run on different Linux and Unix systems without major changes. This makes Bash reliable and widely used for scripting and automation.
- Supports UNIX and Linux systems
- Compatible with older shell scripts
- Includes features from Korn and C Shell
- Useful for scripting and automation
2. Command Options
Bash supports both single-character and multi-character command-line options. These options help users control how commands and scripts behave, making Bash more flexible and easier to use.
- Supports short options like
-aand-c - Supports long options like
--helpand--version - Provides better control over commands
- Makes command execution more flexible
# Short Command Option
ls -l
#Long Command Option
bash --version
3. Bash Launch Files
Bash uses launch files such as .bashrc and .bash_profile to set up the shell environment when Bash starts. These files store configurations, aliases, environment variables, and startup commands.
- Used to configure the Bash environment
- Stores aliases and environment variables
- Automatically runs commands during startup
- Helps customize the terminal experience
#Open .bashrc File
nano ~/.bashrc
#Reload .bashrc File
source ~/.bashrc
4. Interacting shells
An interactive shell allows users to enter commands directly in the terminal and receive immediate output. It helps users interact with the operating system in real time.
- Accepts commands directly from the user
- Displays output instantly
- Useful for executing and testing commands
- Makes terminal interaction easier
5. Conditionals
Bash supports conditional statements that help scripts make decisions based on conditions. Common conditional statements in Bash include if, if-else, and case.
- Used for decision-making in scripts
- Supports
if,else, andcasestatements - Executes commands based on conditions
- Makes scripts more flexible and dynamic
Using if Condition
#!/bin/bash
num=10
if [ $num -gt 5 ]
then
echo "Number is greater than 5"
fi
Using case Statement
#!/bin/bash
fruit="Apple"
case $fruit in
"Apple") echo "Selected Apple" ;;
"Mango") echo "Selected Mango" ;;
esac
6. Shell Arithmetic
Bash supports arithmetic operations that allow users to perform mathematical calculations directly inside scripts. It can handle operations like addition, subtraction, multiplication, and division.
- Performs mathematical calculations in scripts
- Supports addition, subtraction, multiplication, and division
- Helps automate numeric operations
- Makes scripts more powerful and efficient
#!/bin/bash
a=10
b=5
sum=$((a+b))
echo $sum
7. Aliases
Aliases are shortcuts for long or frequently used commands in Bash. They help users save time and make command execution easier.
- Creates shortcuts for commands
- Reduces typing effort
- Saves time while working in the terminal
- Makes command usage simpler and faster
#Create an Alias
alias ll='ls -l'
#Use the Alias
ll
8. Lists
Bash supports lists and arrays that allow users to store and manage multiple values together. Lists make it easier to organize and access data in scripts.
- Stores multiple values in a single variable
- Helps organize data efficiently
- Makes data handling easier in scripts
- Supports accessing values using indexes
#Create a List
fruits=("Apple" "Mango" "Banana")
#Access List Elements
echo ${fruits[0]}
echo ${fruits[1]}