# Bash Scripting for Security

> Automate Linux tasks with Bash scripts: loops, conditionals, pipelines and quick one-off tools for log triage and recon in labs. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/cybersecurity/bash-scripting
Last updated: 2026-11-28

Automate Linux tasks with Bash scripts: loops, conditionals, pipelines and quick one-off tools for log triage and recon in labs.

Beginner · about 12 hours · difficulty 2/5

Learn first: [Linux Command Line Essentials](https://www.internhack.in/roadmaps/tracks/cybersecurity/linux-cli)

If you live on Linux servers, **Bash** is the fastest way to glue tools together. Many analysts and pentesters write small Bash scripts daily even if Python is their main language.

### Core skills

- Shebang (`#!/usr/bin/env bash`), making scripts executable with `chmod +x`.
- Variables, quoting (`"$var"` almost always), arguments (`$1`, `$@`).
- Conditionals with `[[ ... ]]`, loops over files or lines, functions.
- Exit codes and `set -euo pipefail` for safer scripts.
- Combining `grep`, `awk`, `sed`, `sort`, `uniq`, `xargs` and `jq` (for JSON).

### Security uses

- Loop over a list of lab hosts and check which respond on port 22.
- Summarize web server logs by status code and IP.
- Schedule integrity checks with cron and alert on changed hashes.

### When to switch to Python

Once you need data structures, API calls with error handling, or more than about 100 lines, Python becomes easier to maintain.

### Pitfalls

- Unquoted variables that break on spaces or allow injection.
- Parsing `ls` output instead of using globs or `find`.

Move on when you can write a script that accepts a log path, validates it exists, and prints a sorted top-ten report of client IPs.

## Key concepts

- **Variables, quoting and arguments**
- **Conditionals and loops**
- **Exit codes and set -euo pipefail**
- **sed, awk and xargs**
- **jq for JSON**
- **Scheduling with cron**

## Other options for this step

- [Python for Security](https://www.internhack.in/roadmaps/tracks/cybersecurity/python-basics): Learn enough Python to parse logs, call APIs, work with files and sockets, and write your own small security tools.
- [PowerShell for Security](https://www.internhack.in/roadmaps/tracks/cybersecurity/powershell): Use PowerShell to query Windows systems, event logs and Active Directory, and understand why attackers love it too.

## Mini project

Write a Bash script that hashes every file under a directory, stores a baseline, and on later runs reports added, removed or changed files.

## Check yourself

What problems does set -euo pipefail prevent in a Bash script?

## Free resources

- [GNU Bash Reference Manual](https://www.gnu.org/software/bash/manual/) (GNU)
- [ShellCheck](https://www.shellcheck.net/) (ShellCheck)
- [OverTheWire: Bandit](https://overthewire.org/wargames/bandit/) (OverTheWire)

Previous: [Python for Security](https://www.internhack.in/roadmaps/tracks/cybersecurity/python-basics) · Next: [PowerShell for Security](https://www.internhack.in/roadmaps/tracks/cybersecurity/powershell)

## More in Scripting and Automation

- [Python for Security](https://www.internhack.in/roadmaps/tracks/cybersecurity/python-basics)
- [Bash Scripting for Security](https://www.internhack.in/roadmaps/tracks/cybersecurity/bash-scripting)
- [PowerShell for Security](https://www.internhack.in/roadmaps/tracks/cybersecurity/powershell)
- [Automating Security Tasks and APIs](https://www.internhack.in/roadmaps/tracks/cybersecurity/security-automation)
- [Git and GitHub for Security Work](https://www.internhack.in/roadmaps/tracks/cybersecurity/git)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Cybersecurity Roadmap](https://www.internhack.in/roadmaps/tracks/cybersecurity)
