Bash (Bourne Again SHell) is one of the most important tools every developer, DevOps engineer, system administrator, and backend engineer must master. Bash is not just a command-line interface—it is a powerful scripting language used to automate systems, deploy applications, manage servers, and glue entire infrastructures together.
This article is a complete Bash scripting reference, covering everything from fundamentals to advanced scripting techniques.
1. What Is Bash?
Bash is:
- A shell (command interpreter)
- A scripting language
- The default shell on most Linux systems
- Available on macOS and Windows (WSL, Git Bash)
Bash executes commands entered by the user or stored inside scripts.
2. Bash vs Shell
-
Shell is a general term for command interpreters (
sh,zsh,fish) -
Bash is an enhanced version of the original Bourne Shell (
sh) -
Bash adds:
- Arrays
- Command history
- Job control
- Advanced string manipulation
3. Bash Script File Basics
A Bash script is a text file containing Bash commands.
File extension
script.sh
Shebang (Mandatory)
#!/bin/bash
This tells the OS which interpreter to use.
4. Making a Script Executable
chmod +x script.sh
Run it:
./script.sh
5. Comments in Bash
# This is a comment
No multi-line comment syntax exists natively.
6. Variables in Bash
Variable Declaration
name="Farhad"
age=25
❗ No spaces around =.
Accessing Variables
echo $name
echo ${age}
7. Environment Variables
export PATH=$PATH:/custom/path
View:
printenv
8. Read User Input
read username
echo "Hello $username"
With prompt:
read -p "Enter name: " name
9. Positional Parameters
./script.sh arg1 arg2
Inside script:
$0 # script name
$1 # first argument
$2 # second argument
$@ # all arguments
$# # number of arguments
10. Exit Codes
Every command returns a status code:
-
0→ success - Non-zero → failure
echo $?
Exit manually:
exit 1
11. Conditional Statements
if
if [ $age -gt 18 ]; then
echo "Adult"
fi
if-else
if [ "$name" == "Ali" ]; then
echo "Hello Ali"
else
echo "Unknown user"
fi
elif
if [ $age -lt 18 ]; then
echo "Minor"
elif [ $age -lt 60 ]; then
echo "Adult"
else
echo "Senior"
fi
12. Comparison Operators
Numeric
-
-eqequal -
-nenot equal -
-gtgreater than -
-ltless than -
-gegreater or equal -
-leless or equal
String
-
=equal -
!=not equal -
-zempty -
-nnot empty
13. Logical Operators
[ condition1 ] && [ condition2 ]
[ condition1 ] || [ condition2 ]
! condition
14. Case Statement
case $option in
start)
echo "Starting"
;;
stop)
echo "Stopping"
;;
*)
echo "Invalid option"
;;
esac
15. Loops
for loop
for i in 1 2 3; do
echo $i
done
Range:
for i in {1..5}; do
echo $i
done
while loop
while [ $count -lt 5 ]; do
echo $count
((count++))
done
until loop
until [ $count -gt 5 ]; do
echo $count
((count++))
done
16. break and continue
break
continue
17. Functions
my_function() {
echo "Hello"
}
Call:
my_function
With arguments:
greet() {
echo "Hello $1"
}
greet Farhad
18. Return Values from Functions
return 0
Retrieve:
$?
19. Arrays
arr=(apple banana orange)
Access:
echo ${arr[0]}
All elements:
echo ${arr[@]}
Length:
echo ${#arr[@]}
20. Associative Arrays
declare -A user
user[name]="Farhad"
user[age]=25
21. String Manipulation
str="Hello World"
Length:
${#str}
Substring:
${str:0:5}
Replace:
${str/World/Bash}
22. Arithmetic Operations
((a=5+3))
echo $a
Or:
expr 5 + 3
23. Command Substitution
date=$(date)
Or:
date=`date`
24. Input / Output Redirection
Redirect output
ls > file.txt
Append:
ls >> file.txt
Redirect input
wc -l < file.txt
25. Pipes
ps aux | grep root
26. File Test Operators
-f file # regular file
-d dir # directory
-e file # exists
-r file # readable
-w file # writable
-x file # executable
27. Signals and Traps
trap "echo Interrupted" SIGINT
28. Debugging Bash Scripts
Run with debug mode:
bash -x script.sh
Enable inside script:
set -x
Stop:
set +x
29. Error Handling
set -e
Exit on error.
Custom handling:
command || echo "Failed"
30. Process Management
Background:
command &
Wait:
wait
PID:
$$
31. Cron Jobs (Automation)
Edit cron:
crontab -e
Example:
0 2 * * * /path/script.sh
32. Best Practices
- Always use
#!/bin/bash - Quote variables
"${var}" - Use
set -euo pipefail - Use functions
- Avoid hard-coded paths
- Log outputs
33. Real-World Use Cases
- Deployment scripts
- Server monitoring
- Log rotation
- CI/CD pipelines
- Backup automation
- DevOps orchestration
34. Final Thoughts
Bash scripting is not optional for serious developers and engineers. It is the backbone of Linux systems, cloud infrastructure, and DevOps workflows. Mastering Bash gives you control over the operating system itself.
If you understand Bash deeply, you control the machine—not the other way around.
Top comments (2)
Thank you for sharing this article!
As a beginner with Bash scripting, I found it really helpful and practical.
have a good job my friend