C

Linux › Module 6 › Lesson 4

BeginnerModule 6Lesson 4/6

Bash Scripting Basics

Write your first bash scripts with variables, if, and loops

15 min+40 XP3 quiz
Module progress4 of 6

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

1

#!/bin/bash at the top of a script is called:

Multiple choice

Knowledge Check

2

Before ./script.sh you often need:

Multiple choice

Knowledge Check

3

Variables in bash are referenced as:

Multiple choice

← Previous

Answer all 3 knowledge checks to continue. (0/3 answered)