Lesson 17 — Bash Scripting Basics

CompTIA Cyber Path • Linux / Admin Basics • Lesson 17

Lesson 17 — Bash Scripting Basics

shebang + echo + variables + if + chmod +x

A Bash script is just a text file full of commands that Linux can run in sequence. This lesson teaches the basics of writing and running simple scripts using a shebang, printing output with echo, storing values in variables, making basic decisions with if, and making a script executable with chmod +x.

Core idea:

Good scripting follows a simple pattern: write clear commandstest the script manuallymake it executablerun and verify the result.

What you’ll be able to do:

Create a basic Bash script, print useful output, use a variable, build a simple conditional, fix execute permission issues, and run the script successfully.

Linux basics shebang + echo variables + if execute + local progress
Progress: 0%
Write Scriptadd shebang + commands
Add Logicvariables + if
Make Executablechmod +x
Run & Verify./script.sh
Good workflow: Write clearlyadd simple logicset execute permissionrun and verify the output

Lesson Objectives

By the end of this lesson

  • Understand what a Bash script is
  • Start a script with a shebang
  • Use echo to print output
  • Create and use simple variables
  • Write a basic if statement
  • Run a script after making it executable

Why this matters

  • Scripting saves time on repeated tasks
  • Even simple scripts are useful for support work
  • Most automation begins with basic scripts, not giant programs

1) Start with a Shebang

What the shebang does

A shebang tells Linux which interpreter should run the script.

#!/bin/bash

Simple script example

#!/bin/bash echo “Hello from Bash”

This is one of the simplest valid Bash scripts.

Why it matters

The shebang makes the script clearer and helps Linux know how to run it correctly.

Important habit:

Start simple. A useful script that is easy to understand beats a complicated script you cannot troubleshoot.

2) Print Output with echo

echo is one of the most common Bash commands because it lets you show messages and values clearly.

Basic output

echo “Backup started”

This prints a message to the terminal.

Why echo helps

  • Makes scripts easier to follow
  • Helps during testing
  • Shows useful status messages

3) Use Variables

Variables let you store values and reuse them inside the script.

Basic variable example

#!/bin/bash NAME=”Matt” echo “Hello, $NAME”

Here, the variable NAME stores a value that gets reused in the script.

Why variables matter

  • Make scripts easier to update
  • Reduce repeated hard-coded values
  • Improve readability
Real-world habit:

Good scripts avoid repeating the same value in multiple places when one variable can hold it clearly.

4) Make Decisions with if

Basic conditionals let your script respond differently depending on what is true.

Simple if example

#!/bin/bash FILE=”notes.txt” if [ -f “$FILE” ]; then echo “File exists” else echo “File not found” fi

This checks whether a file exists and prints a message based on the result.

Why this matters

Scripts become much more useful when they can check a condition instead of always doing the same thing blindly.

5) Make the Script Executable and Run It

After writing the script, you usually need to give it execute permission.

Grant execute permission

chmod +x myscript.sh

This allows the script to be run directly.

Run the script

./myscript.sh

This runs the script from the current directory.

Common beginner issue

If you see Permission denied, the script may be missing execute permission. Check it with ls -l and fix it with chmod +x.

6) Fast Troubleshooting Pattern

  1. Write a clear script with a shebang
  2. Test the commands inside it
  3. Use echo messages to make output obvious
  4. Make the file executable
  5. Run the script and verify the result
nano myscript.sh chmod +x myscript.sh ./myscript.sh

Practical — Build a Simple Health Check Script

This practical teaches a real support workflow instead of random command memorization.

Practical Task

  • Create a script with a shebang
  • Use echo to print a status message
  • Create one variable
  • Use a basic if check on a file or directory
  • Make the script executable and run it

Walkthrough

#!/bin/bash DIR=”/var/log” echo “Checking directory: $DIR” if [ -d “$DIR” ]; then echo “Directory exists” else echo “Directory missing” fi
chmod +x healthcheck.sh ./healthcheck.sh

The goal is to write something small, readable, and useful. Do not chase complexity yet. Learn the structure first.

Example Ticket Note

Created and tested a basic Bash script with shebang, output messages, variable usage, and simple conditional logic. Applied execute permission and verified successful script execution.

Write Your Observation

Use a simple note like a junior admin would write.

Mini Knowledge Check

Score 75% or higher to unlock the next lesson link. Your score is saved on this browser.

1) What is the purpose of a shebang like #!/bin/bash?

2) What does echo commonly do in a script?

3) Which command commonly fixes a script that says Permission denied?

4) What is the strongest scripting habit from this lesson?

Next Lesson

🔒 Locked until quiz ≥ 75% and you mark complete
Lesson 18 — Users, Groups & Permissions Troubleshooting Lab

Tip: update the next lesson link when your page exists.

Leave a Comment