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.
Good scripting follows a simple pattern: write clear commands → test the script manually → make it executable → run and verify the result.
Create a basic Bash script, print useful output, use a variable, build a simple conditional, fix execute permission issues, and run the script successfully.
Lesson Objectives
By the end of this lesson
- Understand what a Bash script is
- Start a script with a shebang
- Use
echoto print output - Create and use simple variables
- Write a basic
ifstatement - 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.
Simple script example
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.
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
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
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
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
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
This allows the script to be run directly.
Run the script
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
- Write a clear script with a shebang
- Test the commands inside it
- Use
echomessages to make output obvious - Make the file executable
- Run the script and verify the result
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
echoto print a status message - Create one variable
- Use a basic
ifcheck on a file or directory - Make the script executable and run it
Walkthrough
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
Tip: update the next lesson link when your page exists.