Lesson 4 of 8

Bash Scripting Fundamentals

Bash scripting allows you to automate repetitive tasks, build custom security tools, and chain together commands into powerful workflows. You will learn variables, conditionals, loops, functions, and how to process command output — the building blocks of every security automation script. The ability to write bash scripts dramatically multiplies your effectiveness as a security professional.

Learning Objectives — Deep Dive

Each objective below is broken down into plain English so you know exactly what you are learning, why it matters, and what you will be able to do when you are done. Mark each one complete as you work through the lesson.

0/4
In plain English

In simple terms: Write Bash scripts to automate security tasks. This means building enough understanding that you can apply this concept in a real scenario — not just answer a quiz question about it, but actually use it when it matters. Focus on the "why" behind the concept, not just the "what."

Why this matters

Almost every professional security tool runs on Linux, and almost every server you will ever test runs Linux. Fluency in the terminal is not optional — it is the foundation that every other skill in this field is built on.

Real-world analogy

Think of it like learning to drive. You do not need to understand how the engine works to drive safely, but knowing the basics helps you diagnose problems and avoid mistakes that could cause accidents. The same principle applies here — foundational knowledge prevents costly errors.

What you will be able to do

Navigate a Linux system confidently from the command line, write basic automation scripts to speed up repetitive tasks, and use the terminal-based security tools that are standard in professional penetration testing.

Beginner tip

Do not try to memorize everything at once. Read through the material once to get the big picture, then go back and work through the lab. The hands-on practice is what makes the concepts stick — passive reading alone will not get you there.

Bash Scripting Fundamentals · Objective 1 of 4

VIDEO
Loading video…
1

Write a simple recon script

Create a new file called recon.sh with 'nano recon.sh' and write a bash script that automates basic reconnaissance: run 'nmap -sV $1' to scan a target passed as the first argument, save the output to a file, and print a summary. The shebang line '#!/bin/bash' must be the first line of every bash script — it tells the kernel which interpreter to use. Use variables ($1, $2) for arguments, echo statements to show progress, and comments (lines starting with #) to document what each section does. A well-commented script is readable by others and by your future self.

nano recon.sh
2

Make it executable

Run 'chmod +x recon.sh' to add the execute permission to your script, then run './recon.sh' to test it. The './' prefix tells the shell to look for the script in the current directory rather than in the system PATH — without it, the shell will report 'command not found' even if the file exists. If you want to run the script from any directory without the './' prefix, move it to /usr/local/bin/ with 'sudo mv recon.sh /usr/local/bin/recon'. Verify the script is executable by running 'ls -la recon.sh' and confirming the 'x' bit is set.

chmod +x recon.sh && ls -la recon.sh
3

Parse nmap output

Modify your script to save nmap output in grepable format with 'nmap -sV -oG scan.gnmap $1' and then parse it with grep and awk to extract only open ports. The grepable format puts each host on one line, making it easy to process with standard Unix tools. For example, 'grep "open" scan.gnmap | awk "{print $2, $5}"' extracts IP addresses and open port/service pairs. Practice parsing different nmap output formats — XML output (-oX) can be parsed with xmllint or Python's xml library for more complex automation.

nmap -sV -oG scan.gnmap $1 && grep 'open' scan.gnmap
4

Run the script

Run './recon.sh <metasploitable-ip>' against your Metasploitable VM and verify the output is correct and well-formatted. Check that the output file was created and contains the expected data. Test edge cases: what happens if you run the script with no arguments? Add input validation at the top of the script: 'if [ -z "$1" ]; then echo "Usage: $0 <target>"; exit 1; fi'. Good scripts fail gracefully with helpful error messages rather than producing confusing output or crashing silently.

./recon.sh 192.168.56.101