In a nutshell A bash script is just a file of shell commands run top to bottom. The moment you're doing the same commands twice, a script does them for you — sweeping a range of hosts, trying a list of things, automating a check. For a security person it's the glue that turns manual steps into a repeatable tool.
From typing commands to saving them
#!/bin/bash # the shebang: which interpreter runs this file
echo "Starting scan..."
nmap -sV 10.10.10.5
echo "Done."
chmod +x scan.sh # make it runnable
./scan.sh # run it
Analogy — a recipe card. Instead of remembering each step every time you cook, you write the recipe once and follow it exactly, without forgetting a step.
The building blocks
target="10.10.10.5" # variable; $target reads it
echo "Scanning $target"
echo "Pinging $1" # $1 = the first argument you pass in
ping -c 1 "$1" # ./ping.sh 10.10.10.5
if ping -c 1 "$1" &>/dev/null; then # conditional
echo "$1 is up"
fi
The real power for security — loops
# ping-sweep a whole /24 in one loop
for i in $(seq 1 254); do
ping -c 1 -W 1 10.10.10.$i &>/dev/null && echo "10.10.10.$i is up"
done
# try each word from a file
while read word; do
echo "trying $word"
done < wordlist.txt
The pipe — the most important operator
The pipe | sends one command's output into the next, building complex processing from simple parts in one line:
Why it matters in security
- Automate the boring, repeatable parts — sweep a network, hit many URLs, parse tool output.
- Read what you find — you will meet bash scripts on targets (cron jobs, backup scripts). Reading them spots a privesc path — a root cron running a writable script (see Linux privilege escalation).
- Glue tools together — pipe one into another, loop over a list, and you've built a small custom tool.
Safety habits
Quote your variables (
"$1") — an unquoted value with spaces or special characters breaks things or injects commands. Put set -e near the top so the script stops on the first error. Test on safe input first — a loop that deletes or connects, fed the wrong list, does damage fast.