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 #).

bash
#!/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"
Saves the date in a variable, makes a folder, copies logs in.

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.

bash — real output
$ chmod +x backup.sh
$ ./backup.sh
Backed up logs into backups/2026-11-02
The script runs and reports where it saved the files.

Variables and conditions

bash
#!/bin/bash
host="8.8.8.8"
if ping -c 1 "$host" > /dev/null; then
  echo "$host is reachable"
else
  echo "$host is DOWN"
fi
Pings a host once and reports up or down — a tiny monitor.
Always put variables in double quotes ("$host", not $host). It saves you from strange bugs when a value contains a space.
All articles