Master Bash Scripting: Variables, Loops, Functions, and More
This guide introduces Bash scripting fundamentals, covering variable declaration, printing with echo and printf, comments, user input, arrays, conditional statements, loops, functions, and tips for extending scripts with other languages, enabling readers to automate tasks on Unix-like systems.
Bash Scripting Overview
Bash scripts allow you to automate a wide range of tasks on Unix-like operating systems without installing additional dependencies. They are portable, lightweight, and essential for developers working with command‑line environments.
Variables
Bash variables are case‑sensitive and are assigned using the = operator without spaces. To reference a variable, prepend $.
STATE=LinuxMi STATE="Ubuntu Linux" echo $STATEPrinting Values
You can output text with echo for simple messages or printf for formatted strings. Example:
STATE=LinuxMi
printf "My Site is %s
" $STATECommon printf format verbs include: %s – string %d – integer %f – floating‑point number %x – hexadecimal (lowercase) %% – literal percent sign
Comments
Use # at the beginning of a line to add a comment. Multi‑line comments are not supported, but most editors provide shortcuts to comment multiple lines.
# This is a commentUser Input
The read command captures input from the user and stores it in a variable.
echo -n "What do you want? "
read response
echo $responseArrays
Declare an array by listing elements within parentheses. Access the entire array with ${array[*]} or a specific element with an index.
Countries=('Ubuntu' 'Debian' 'CentOS' "openSUSE" "Linuxmi.com")
echo ${Countries[*]}
echo "${Countries[2]}"Conditional Statements
Use if‑elif‑else for branching logic, ending with fi. The case statement matches a variable against patterns, ending with esac.
if [[ $a -gt $b ]]; then
echo "a is greater"
elif [[ $a -lt $b ]]; then
echo "b is greater"
else
echo "equal"
fi NAME=LinuxMi
case $NAME in
"Debian") echo "Debian is popular" ;;
"LinuxMi"|"Ubuntu") echo "OpenSUSE" ;;
*) echo "default case" ;;
esacLoops
Bash supports C‑style for loops, range for loops, and while loops.
for ((i=0; i<10; i+=2)); do
echo $i
done for i in {1..7}; do
echo $i
done counter=1
while [ $counter -le 5 ]; do
echo $counter
((counter++))
doneFunctions
Define a function by writing its name followed by parentheses and a block of commands. Use return to exit with a status code; to output a value, use echo.
print_working_directory() {
echo "$PWD"
}
print_working_directoryBeyond Bash
While Bash is powerful for quick automation, other languages such as Python, Go, Ruby, and Rust provide richer features and may be preferable for larger projects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
