Linux › Module 6 › Lesson 4
Bash Scripting Basics
Write your first bash scripts with variables, if, and loops
Opening
Stop repeating yourself
Ran the same five commands twice today? Write a script. Bash scripts are plain text files that run commands in order—automation for pentesters and sysadmins alike.
1. Hello Script
nano hello.sh #!/bin/bash echo "Hello from Cyberlium" whoami date chmod +x hello.sh ./hello.sh
2. Variables
NAME="analyst" echo "User: \$NAME" LOG="/var/log/syslog" wc -l < "\$LOG"
3. if and Loops
if [ -f "/etc/passwd" ]; then echo "passwd exists" fi for f in *.txt; do echo "File: \$f" done
Shebang matters
#!/bin/bash as line 1 tells Linux which interpreter to use. Without chmod +x, run: bash hello.sh
Knowledge Check
#!/bin/bash at the top of a script is called:
Multiple choice
Knowledge Check
Before ./script.sh you often need:
Multiple choice
Knowledge Check
Variables in bash are referenced as:
Multiple choice