Intermediate · 9 min read · Updated 2026-11
Automate with Bash scripts
Turn a sequence of commands you type every day into one script you run with a single word.
A Bash script is just a text file full of the same commands you'd type by hand, run top to bottom. If you do something more than twice, it's worth scripting. Create a file ending in .sh and start it with a special first line.
Your first script
The first line, called the shebang, tells the system which program should run the file. Then you add commands and comments (lines starting with #).
#!/bin/bash
# backup.sh — copy today's logs into a dated folder
today=$(date +%Y-%m-%d)
mkdir -p "backups/$today"
cp *.log "backups/$today/"
echo "Backed up logs into backups/$today"Run it
Make the file executable once with chmod, then run it. $(…) runs a command and drops its output into your line — here, today's date.
$ chmod +x backup.sh $ ./backup.sh Backed up logs into backups/2026-11-02
Variables and conditions
#!/bin/bash
host="8.8.8.8"
if ping -c 1 "$host" > /dev/null; then
echo "$host is reachable"
else
echo "$host is DOWN"
fiAlways put variables in double quotes ("$host", not $host). It saves you from strange bugs when a value contains a space.