Shell scripting is one of the most powerful—and approachable—skills in the Linux ecosystem. Modern shells such as Bash, Zsh, Tcsh, and Ksh are not only command interpreters but full programming environments. With just a few lines of code, you can automate repetitive tasks, manage systems, and glue complex workflows together.
Below are 40 simple yet effective Bash script examples, organized to help you quickly apply them to real-world scenarios.
🚀 Getting Started #
1. Hello World #
#!/bin/bash
echo "Hello World"
2. Printing with echo
#
#!/bin/bash
echo "Printing text"
echo -n "Printing text without newline"
echo -e "\nRemoving \t special \t characters\n"
3. Using Comments #
#!/bin/bash
# Adding two values
((sum=25+35))
echo $sum
4. Multi-line Comments #
#!/bin/bash
: '
This script calculates
the square of 5.
'
((area=5*5))
echo $area
🔁 Loops & Control Flow #
5. While Loop #
#!/bin/bash
i=0
while [ $i -le 2 ]; do
echo Number: $i
((i++))
done
6. For Loop #
#!/bin/bash
for ((counter=1; counter<=10; counter++)); do
echo -n "$counter "
done
echo
7. User Input #
#!/bin/bash
read -p "Enter Something: " something
echo "You Entered: $something"
8. If Statement #
#!/bin/bash
read -p "Enter a number: " num
if [[ $num -gt 10 ]]; then
echo "Number is greater than 10."
fi
9. If–Else Statement #
#!/bin/bash
read n
if [ $n -lt 10 ]; then
echo "One digit number"
else
echo "Two digit number"
fi
10. Logical AND #
#!/bin/bash
read -p "Enter Number: " num
if [[ $num -lt 10 && $((num % 2)) -eq 0 ]]; then
echo "Even Number"
else
echo "Odd Number"
fi
11. Logical OR #
#!/bin/bash
read -p "Enter any number: " n
if [[ $n -eq 15 || $n -eq 45 ]]; then
echo "You won"
else
echo "You lost!"
fi
12. Elif Chain #
#!/bin/bash
read -p "Enter a number: " num
if [[ $num -gt 10 ]]; then
echo "Greater than 10"
elif [[ $num -eq 10 ]]; then
echo "Equal to 10"
else
echo "Less than 10"
fi
13. Case Statement #
#!/bin/bash
read -p "Enter a number: " num
case $num in
100) echo "Hundred" ;;
200) echo "Two Hundred" ;;
*) echo "Other number" ;;
esac
🧩 Arguments & Strings #
14. Command-Line Arguments #
#!/bin/bash
echo "Total arguments: $#"
echo "First: $1"
echo "Second: $2"
15. Named Arguments #
#!/bin/bash
for arg in "$@"; do
key=${arg%=*}
val=${arg#*=}
case $key in
X) x=$val ;;
Y) y=$val ;;
esac
done
echo "X+Y=$((x+y))"
16. String Concatenation #
#!/bin/bash
string1="Ubuntu"
string2="Pit"
echo "$string1$string2 is a great resource."
17. String Slicing #
#!/bin/bash
Str="Learn Bash Commands from UbuntuPit"
echo "${Str:0:20}"
18. String Slicing with cut
#
#!/bin/bash
Str="Learn Bash Commands from UbuntuPit"
echo "$Str" | cut -d ' ' -f 1-3
🧮 Arithmetic & Functions #
19. Add Two Numbers #
#!/bin/bash
read -p "First: " x
read -p "Second: " y
echo "Sum=$((x+y))"
20. Add Multiple Numbers #
#!/bin/bash
sum=0
for i in {1..4}; do
read -p "Enter number: " n
((sum+=n))
done
echo "Total: $sum"
21. Functions #
#!/bin/bash
Add() {
read -p "A: " x
read -p "B: " y
echo "Sum=$((x+y))"
}
Add
22. Function with Return Value #
#!/bin/bash
Greet() {
echo "Hello $1, welcome!"
}
read name
msg=$(Greet "$name")
echo "$msg"
📁 Files & Directories #
23. Create Directory #
#!/bin/bash
read -p "Directory name: " dir
mkdir "$dir"
24. Create Directory If Missing #
#!/bin/bash
read -p "Directory name: " dir
[ -d "$dir" ] || mkdir "$dir"
25. Read File Line by Line #
#!/bin/bash
while read line; do
echo "$line"
done < editors.txt
26. Delete File Safely #
#!/bin/bash
read -p "Filename: " name
rm -i "$name"
27. Append to File #
#!/bin/bash
echo "6. NotePad++" >> editors.txt
28. Test File Existence #
#!/bin/bash
[ -f "$1" ] && echo "File exists" || echo "File not found"
🕒 Processes & System Tasks #
29. Send Mail #
#!/bin/bash
mail -s "Greetings" admin@example.com <<< "Welcome"
30. Parse Date #
#!/bin/bash
date +"%d-%m-%Y"
31. Sleep #
#!/bin/bash
sleep 5
echo "Waited 5 seconds"
32. Wait for Process #
#!/bin/bash
sleep 5 &
pid=$!
wait $pid
echo "$pid finished"
33. Last Modified File #
#!/bin/bash
ls -lrt | awk 'END{print $NF}'
34. Batch Rename Files #
#!/bin/bash
for file in "$1"/*; do
mv "$file" "$file.UP"
done
35. Count Files & Folders #
#!/bin/bash
echo "Files: $(find "$1" -type f | wc -l)"
echo "Dirs: $(find "$1" -type d | wc -l)"
36. Clean Logs (Root) #
#!/bin/bash
: > /var/log/messages
echo "Logs cleaned"
37. Backup Recent Files #
#!/bin/bash
tar rvf backup-$(date +%F).tar $(find . -mtime -1 -type f)
38. Check Root User #
#!/bin/bash
[ "$UID" -eq 0 ] && echo "Root user" || echo "Not root"
39. Remove Duplicate Lines #
#!/bin/bash
sort "$1" | uniq > cleaned.txt
40. System Maintenance (Debian/Ubuntu) #
#!/bin/bash
apt-get update &&
apt-get -y upgrade &&
apt-get -y autoremove &&
apt-get autoclean
These 40 examples form a practical foundation for everyday Bash scripting. Once mastered, they allow you to automate workflows, enforce consistency, and dramatically improve productivity across Linux systems.