Learn Bash - 10 Code Examples & CST Typing Practice Test
Bash (Bourne Again SHell) is a Unix shell and command language widely used for automation, scripting, DevOps, system administration, and shell-based application workflows. It is the default shell on most Linux systems and offers powerful command-line capabilities.
Learn BASH with Real Code Examples
Updated Nov 19, 2025
Code Sample Descriptions
Bash Counter and Theme Toggle
count=0
isDark=0
updateUI() {
echo "Counter: $count"
echo "Theme: $( [ $isDark -eq 1 ] && echo 'Dark' || echo 'Light' )"
}
increment() { count=$((count + 1)); updateUI; }
decrement() { count=$((count - 1)); updateUI; }
reset() { count=0; updateUI; }
toggleTheme() { isDark=$((1 - isDark)); updateUI; }
# Simulate actions
updateUI
increment
increment
toggleTheme
decrement
reset
Demonstrates a simple counter with theme toggling using Bash variables and console output.
Bash Simple Calculator
add() { echo $(( $1 + $2 )); }
subtract() { echo $(( $1 - $2 )); }
multiply() { echo $(( $1 * $2 )); }
divide() { echo $(( $1 / $2 )); }
echo "Add 5 + 3: $(add 5 3)"
echo "Subtract 5 - 3: $(subtract 5 3)"
echo "Multiply 5 * 3: $(multiply 5 3)"
echo "Divide 6 / 2: $(divide 6 2)"
Performs basic arithmetic operations using functions.
Bash Factorial
factorial() {
if [ $1 -le 1 ]; then
echo 1
else
echo $(( $1 * $(factorial $(( $1 - 1 ))) ))
fi
}
echo "Factorial of 5: $(factorial 5)"
Recursive factorial calculation.
Bash Fibonacci Sequence
fibonacci() {
if [ $1 -eq 0 ]; then
echo 0
elif [ $1 -eq 1 ]; then
echo 1
else
echo $(( $(fibonacci $(( $1 - 1 ))) + $(fibonacci $(( $1 - 2 ))) ))
fi
}
for i in {0..9}; do
echo $(fibonacci $i)
done
Generate Fibonacci numbers using recursion.
Bash Array Comprehension Simulation
numbers=(1 2 3 4 5 6 7 8 9 10)
squares=()
for n in "${numbers[@]}"; do
if [ $((n % 2)) -eq 0 ]; then
squares+=( $((n * n)) )
fi
done
echo "${squares[@]}"
Create array of squares for even numbers.
Bash String Manipulation
str="HelloWorld"
sub=${str:0:5}
len=${#str}
echo "Substring: $sub, Length: $len"
Demonstrates substring extraction and length.
Bash Loop and Conditional
for i in {1..10}; do
if [ $((i % 2)) -eq 0 ]; then
echo "$i is even"
fi
done
Loop through numbers and print even ones.
Bash Functions with Arguments
greet() {
echo "Hello, $1!"
}
greet Alice
greet Bob
Pass arguments to functions and print results.
Bash Sum of Array
numbers=(1 2 3 4 5)
sum=0
for n in "${numbers[@]}"; do
sum=$((sum + n))
done
echo $sum
Sum elements of an array using a loop.
Bash Zip Simulation
arr1=(1 2 3)
arr2=(4 5 6)
for i in ${!arr1[@]}; do
echo "$((arr1[i] + arr2[i]))"
done
Combine two arrays element-wise using a loop.
Frequently Asked Questions about Bash
What is Bash?
Bash (Bourne Again SHell) is a Unix shell and command language widely used for automation, scripting, DevOps, system administration, and shell-based application workflows. It is the default shell on most Linux systems and offers powerful command-line capabilities.
What are the primary use cases for Bash?
System automation. Server maintenance scripts. DevOps & CI/CD pipelines. File management & text processing. Docker and container orchestration. Environment setup. Scheduling cronjobs
What are the strengths of Bash?
Installed nearly everywhere. Perfect for automation. Integrates with all Unix tools. Simple for small scripts. Ideal for CI/CD and DevOps
What are the limitations of Bash?
Not suitable for large-scale applications. Weak type system. Difficult debugging. Portability issues across shells. Limited data structures
How can I practice Bash typing speed?
CodeSpeedTest offers 10+ real Bash code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.