Developer Workflow · #18 of 19
Shell Scripting
Bash Basics
What is a Shell Script?
A shell script is a text file containing commands that run sequentially. Automate anything you do in the terminal!
Your First Script
Create a file hello.sh:
#!/bin/bash
echo "Hello, World!"
The #! (shebang) tells the system which interpreter to use.
Make it Executable
chmod +x hello.sh
./hello.sh # Run it!
Or run directly with bash:
bash hello.sh
Variables
#!/bin/bash
NAME="Alice"
echo "Hello, $NAME"
echo "Hello, ${NAME}!" # Braces for clarity
# Command substitution
TODAY=$(date +%Y-%m-%d)
FILES=$(ls | wc -l)
echo "Today is $TODAY, we have $FILES files"
Special Variables
| Variable | Meaning |
|---|---|
$0 |
Script name |
$1, $2... |
Arguments |
$# |
Number of arguments |
$@ |
All arguments |
$? |
Exit status of last command |
$$ |
Current process ID |
User Input
#!/bin/bash
echo "What's your name?"
read NAME
echo "Hello, $NAME!"
# One-liner with prompt
read -p "Enter your age: " AGE
echo "You are $AGE years old"
Conditionals
if/then/else
#!/bin/bash
if [ -f /etc/passwd ]; then
echo "File exists"
else
echo "File not found"
fi
Test Operators
File tests:
| Test | True if… |
|---|---|
-f file |
File exists (regular file) |
-d dir |
Directory exists |
-e path |
Path exists |
-r file |
File is readable |
-w file |
File is writable |
-x file |
File is executable |
String tests:
| Test | True if… |
|---|---|
-z "$str" |
String is empty |
-n "$str" |
String is not empty |
"$a" = "$b" |
Strings are equal |
"$a" != "$b" |
Strings differ |
Numeric tests:
| Test | True if… |
|---|---|
$a -eq $b |
Equal |
$a -ne $b |
Not equal |
$a -lt $b |
Less than |
$a -gt $b |
Greater than |
$a -le $b |
Less or equal |
$a -ge $b |
Greater or equal |
Loops
for Loop
#!/bin/bash
for i in 1 2 3 4 5; do
echo "Number: $i"
done
# Range
for i in {1..10}; do
echo "Count: $i"
done
# Files
for file in *.txt; do
echo "Processing $file"
done
while Loop
#!/bin/bash
COUNT=0
while [ $COUNT -lt 5 ]; do
echo "Count: $COUNT"
COUNT=$((COUNT + 1))
done
Reading Lines
#!/bin/bash
while read line; do
echo "Line: $line"
done < /etc/passwd
Functions
#!/bin/bash
greet() {
echo "Hello, $1!"
}
greet "Alice"
greet "Bob"
# With return value
is_even() {
if [ $(($1 % 2)) -eq 0 ]; then
return 0 # True
else
return 1 # False
fi
}
if is_even 4; then
echo "4 is even"
fi
Practical Examples
Backup Script
#!/bin/bash
DATE=$(date +%Y%m%d)
tar -czf backup_$DATE.tar.gz /home/$USER/Documents
echo "Backup created: backup_$DATE.tar.gz"
System Info
#!/bin/bash
echo "=== System Info ==="
echo "Hostname: $(hostname)"
echo "Uptime: $(uptime -p)"
echo "Disk Usage: $(df -h / | tail -1 | awk '{print $5}')"
echo "Memory: $(free -h | grep Mem | awk '{print $3"/"$2}')"
Try It!
Practice scripting in the terminal:
Exercises:
- Create a simple script:
echo '#!/bin/bash' > test.sh - Add a command:
echo 'echo "Hello"' >> test.sh - Make executable:
chmod +x test.sh - Run it:
./test.sh
Try it in the terminal
A sandboxed shell with this lesson’s commands. Type help to see what’s available.