- Published on
Essential Bash Crash Course for Software Engineers
- Authors

- Name
- Mehdi Akiki
Reference
Bash is a powerful scripting language essential for automating tasks in Linux. This crash course covers the core elements that every software engineer should know to become effective at Bash scripting.
Basics of Bash
Variables
name="Alice"
echo "Hello, $name"
- Variables in bash are defined without
=spaces. - Use
$to reference a variable.
Conditional Statements
if [ "$name" == "Alice" ]; then
echo "Hello, Alice!"
else
echo "You are not Alice."
fi
[ ... ]is used to test conditions.==compares strings,-eqis for integers.
Loops
- For LoopLoops over a sequence of numbers or list of strings.
for i in {1..5}; do echo "Iteration: $i" done - While Loop
count=0 while [ $count -lt 3 ]; do echo "Count is: $count" count=$((count+1)) done
Functions
function greet() {
local name="$1"
echo "Hello, $name!"
}
greet "Bob"
- Use
functionkeyword (optional) andlocalfor scope isolation. - Call with
greet "argument".
File Operations
- Redirect Output
echo "Hello World" > output.txt>writes (overwrites),>>appends. - Read File Line by Line
while read -r line; do echo "Line: $line" done < input.txt
Command Substitution
current_date=$(date)
echo "Today's date is $current_date"
- Wrap a command in
$(...)to assign its output to a variable.
Control Operators
- AND (
&&) and OR (||)Executes second command only if the first succeeds.mkdir my_folder && echo "Folder created" - Pipes (
|)Passes the output of one command to another.cat file.txt | grep "search_term"
Wildcards & Globbing
echo *.txt # Lists all files ending with .txt
*matches zero or more characters,?matches exactly one.
Debugging Scripts
- Run with Debug ModeEnables debug output for step-by-step tracing.
bash -x script.sh
Practical Tips
- Environment Variables: Use
$HOMEor$USERfor user-specific paths. - Shebang (
#!): Always start your script with#!/bin/bashto specify the interpreter. - Exit Codes: Use
$?to get the status of the last command executed.
Example Script: File Backup
#!/bin/bash
src_folder="/path/to/source"
dest_folder="/path/to/backup"
date=$(date +%Y-%m-%d)
backup_file="$dest_folder/backup-$date.tar.gz"
tar -czf "$backup_file" "$src_folder" && echo "Backup successful: $backup_file"
- Create a compressed backup using
tar. &&ensures echo runs only on success.
FAQ
How do you run a bash script?
- Use
./script.shafter making it executable (chmod +x script.sh).
- Use
What's the difference between
[ ... ]and[[ ... ]]?[[ ... ]]supports more complex expressions and is safer for string comparisons.
How do you pass arguments to a script?
- Use
$1,$2, etc., for positional arguments (script.sh arg1 arg2).
- Use
How do you check if a file exists?
if [ -f "filename.txt" ]; then echo "File exists."; fiWhat is
#!/bin/bash?- It specifies the interpreter used to execute the script.
How do you set a variable as read-only?
readonly my_var="value"How do you loop through files in a directory?
for file in /path/to/dir/*; do echo "File: $file"; doneHow can you handle errors in bash?
- Use
set -eto exit the script when any command fails.
- Use
How do you create an alias?
alias ll='ls -la'for convenient command shortcuts.
How do you declare arrays in bash?
my_array=("item1" "item2" "item3") echo "First item: ${my_array[0]}"