RedTeam Nexus Labs
Module 07 · Lesson 05 // Bash & Shell

scripting basics

Bash & Shell · Linux

Freereading · 10 min
On this page
LESSON

Overview

When you find yourself running the same commands repeatedly, it is time to write a shell script — a file of commands the shell executes in sequence. Scripting turns manual routines into repeatable, shareable automation. This lesson covers the basics of a Bash script.

Learning objectives

  • Write and run a basic Bash script.
  • Explain the shebang and making a script executable.
  • Use variables, conditionals, and loops at a basic level.

Core concept

A shell script is a text file containing commands. Key elements:

  • Shebang — the first line #!/bin/bash tells the system which interpreter to use.
  • Executable bit — make it runnable with chmod +x script.sh, then run ./script.sh.
  • Variables, conditionals (if), and loops (for/while) — give scripts logic beyond a straight list of commands.
  • Comments — lines starting with # document the script.

How it works

The shebang line lets you execute the file directly (./script.sh) rather than typing bash script.sh. The shell reads the file top to bottom, running each command. Control structures add logic: if tests a condition and branches, for/while repeat actions over items or while a condition holds. A minimal script:

#!/bin/bash
for f in *.log; do
  echo "Found log: $f"
done

This iterates over log files and prints each — a simple but genuine automation.

Practical example

#!/bin/bash
# backup.sh — archive the reports directory with a dated filename
name="reports-$(date +%F).tar.gz"
tar -czf "$name" reports/
echo "Created $name"

After chmod +x backup.sh, running ./backup.sh creates a dated archive of the reports directory and prints the filename. The same routine now runs identically every time, with no manual typing.

Cybersecurity relevance

Scripting is a core skill for both defenders and testers: automating log parsing, system checks, and repetitive analysis saves time and reduces error. Blue teams script routine monitoring and response steps; in authorized testing, scripts orchestrate repetitive tasks. Because scripts run commands automatically, they must be written carefully — the next lesson covers safe automation (handling errors, quoting, avoiding destructive mistakes), which is essential before trusting a script with real work.

Common mistakes

  • Omitting the shebang or the executable bit, so the script will not run as ./script.sh.
  • Not quoting variables in scripts, causing failures on spaces/special characters.
  • Running an untested script on important data, risking damage from a logic error.

Key takeaways

  • A shell script is a file of commands with a shebang (#!/bin/bash), made runnable via chmod +x and executed as ./script.sh.
  • Variables, conditionals (if), and loops (for/while) add logic; # marks comments.
  • Scripting automates repetitive security tasks (log parsing, checks); write carefully and apply safe-automation practices before trusting scripts with real data.

Check your understanding

  • What does the shebang line do, and what two steps let you run a script as ./script.sh?
  • Give one reason to automate a repetitive security task with a script rather than doing it by hand.
FINISH THIS LESSON

Sign in to save your progress.