Bash scripting is a core skill for Linux cybersecurity professionals. It allows you to automate tasks, manage systems, and run security tools efficiently. By writing scripts, repetitive tasks can be automated, saving time and reducing errors.
Bash stands for Bourne Again Shell. It is a command-line interpreter that executes commands and scripts in Linux.
Check Bash version:
bash --version
.sh extension:nano hello.sh
#!/bin/bash
echo "Hello, Linux Cybersecurity!"
Ctrl + O, Enter, Ctrl + X).chmod +x hello.sh
./hello.sh
Output:
Hello, Linux Cybersecurity!
Variables store data that can be reused in scripts.
name="CRMNuggets"
echo "Welcome, $name!"
Output:
Welcome, CRMNuggets!
read -p "Enter your username: " user
echo "Hello, $user!"
Common commands used in Bash scripts:
| Command | Purpose |
|---|---|
echo |
Print text or variables |
read |
Get input from user |
if ... then ... fi |
Conditional statements |
for ... do ... done |
Loops |
while ... do ... done |
Loops |
date |
Display current date/time |
ls |
List directory contents |
pwd |
Show current directory |
#!/bin/bash
read -p "Enter a number: " num
if [ $num -gt 10 ]; then
echo "The number is greater than 10."
else
echo "The number is 10 or less."
fi
#!/bin/bash
for i in {1..5}
do
echo "Iteration $i"
done
Output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Bash scripts can automate:
#!/bin/bash
tar -czf backup_$(date +%F).tar.gz /home/student/Documents
echo "Backup completed successfully!"
This script creates a compressed backup of the Documents directory with a timestamp.
Students should now understand: