0Pricing
Ethical Hacking Academy · Lesson

Script Basics

Variables and loops.

Script Basics is a free Ethical Hacking Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ethical Hacking Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Bash for Security

Bash is the default shell on Linux and is everywhere a pentester goes. Scripting it lets you automate repetitive recon, chain tools, and process output without rewriting commands by hand.

This lesson covers the building blocks: shebang, variables, conditionals, and loops.

The Shebang

Every script starts with a shebang telling the system which interpreter to use. Then make the file executable and run it.

#!/bin/bash
echo 'Hello from a script'

# chmod +x script.sh
# ./script.sh

Variables

Assign with no spaces around =. Read back with a dollar sign. Quote variables to avoid word-splitting surprises.

target=192.168.56.10
port=80
echo "Scanning $target on port $port"

Command Substitution

Capture a command's output into a variable using $( ). This is how scripts react to tool results.

host=$(hostname)
count=$(ls /etc | wc -l)
echo "$host has $count files in /etc"

Reading Arguments

Scripts receive arguments as $1, $2, and so on. $# is the count and $0 is the script name.

#!/bin/bash
target=$1
echo "Target is $target"
# run: ./scan.sh 10.0.0.5

Conditionals

Use if with test brackets to branch. Numeric tests use -eq, -gt; string tests use =; file tests use -f, -d.

if [ "$#" -eq 0 ]; then
  echo 'Usage: scan.sh <target>'
  exit 1
fi

For Loops

for loops iterate over lists - perfect for sweeping ports or hosts.

for port in 21 22 80 443; do
  echo "Checking port $port"
done

Looping Over Ranges

Brace expansion and seq generate sequences, handy for scanning an IP range.

for i in {1..10}; do
  echo "192.168.56.$i"
done

While Loops

while repeats as long as a condition holds. Reading a file line by line is a common pattern for processing host or wordlists.

while read line; do
  echo "Host: $line"
done < hosts.txt

Functions and Exit Codes

Group reusable logic into functions. Every command sets $?, its exit code - 0 means success. Scripts use this to decide what to do next.

check() {
  ping -c1 "$1" >/dev/null 2>&1
  if [ $? -eq 0 ]; then echo "$1 up"; fi
}
check 192.168.56.10

Quiet and Safe Scripts

Production-grade scripts redirect noise and fail fast:

  • 2>/dev/null hides errors.
  • set -e exits on the first failed command.
  • set -u errors on undefined variables.
#!/bin/bash
set -euo pipefail

Quick Check

Recall how scripts capture command output.

Recap

You learned Bash scripting fundamentals:

  • Shebang + chmod +x to make a runnable script.
  • Variables, arguments ($1), and $( ) command substitution.
  • if, for, and while for control flow.
  • Exit codes ($?) and set -euo pipefail for robust scripts.

Next: automating recon.

Frequently asked questions

Is the “Script Basics” lesson free?

Yes — the full text of “Script Basics” is free to read here on the web, and the Ethical Hacking Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ethical Hacking Academy course, upgrade to CoddyKit PRO.

What will I learn in “Script Basics”?

Variables and loops. You practise Ethical Hacking Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ethical Hacking Academy?

No prior experience is required. Ethical Hacking Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Script Basics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ethical Hacking Academy lesson?

Yes. Every Ethical Hacking Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Script Basics
  2. Automating Recon
  3. Parsing Output
  4. Building a Recon Tool
← Back to Ethical Hacking Academy