Introduction to Bash Scripting
Learn the fundamentals of Bash scripting, including variables, conditional statements, loops, and basic command execution.
Introduction to Bash Scripting is a free Linux Server Deployment & SSH Mastery lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Linux Server Deployment & SSH Mastery learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is Bash Scripting?
Welcome to the world of Bash scripting! Bash (Bourne Again SHell) is the default command-line interpreter on most Linux systems.
A Bash script is simply a text file containing a series of commands, allowing you to automate tasks that you would normally type manually into the terminal.
- Save time by automating repetitive tasks.
- Execute complex sequences of commands with a single run.
- Build custom tools for server administration.
Your First Script: Hello World
Every script starts with a shebang line (#!/bin/bash). This tells your system which interpreter to use for the script.
Then, you add your commands. Let's create a classic 'Hello, World!' script.
#!/bin/bash
# This is a comment: The script says hello.
echo "Hello, CoddyKit!"
Making Your Script Executable
After saving your script (e.g., as hello.sh), you need to give it permission to run. This is done using the chmod command, which changes file permissions.
The +x option grants execute permission. Without it, your system won't know it's a program to run.
#!/bin/bash
# In a real terminal, you would run:
# chmod +x hello.sh
# ./hello.sh
echo "Permissions matter!"
Working with Variables
Variables store data. In Bash, you define a variable by assigning a value to a name. No special declaration keyword is needed.
- Assign:
NAME=value(no spaces around=) - Access:
$NAMEor${NAME}
Let's store a message and print it.
#!/bin/bash
MESSAGE="Welcome to scripting!"
echo $MESSAGE
Combining Commands & Variables
You can use variables with standard Linux commands. For instance, you can store a directory name in a variable and then use ls to list its contents.
This makes scripts flexible, as you can change the variable's value without editing the command itself.
#!/bin/bash
TARGET_DIR="/tmp"
echo "Listing contents of $TARGET_DIR:"
ls $TARGET_DIR
Conditional Logic: The 'if' Statement
Scripts often need to make decisions. The if statement allows your script to execute code only if a certain condition is true.
Conditions are usually enclosed in square brackets [ ]. Remember spaces around the brackets and operators!
#!/bin/bash
NUMBER=10
if [ $NUMBER -gt 5 ]; then
echo "Number is greater than 5."
fi
Adding 'else' and 'elif'
To handle cases where the if condition is false, you can use else. If you have multiple conditions, elif (else if) comes in handy.
-eq: equals-ne: not equals-lt: less than-le: less than or equal to-gt: greater than-ge: greater than or equal to
#!/bin/bash
GRADE=85
if [ $GRADE -ge 90 ]; then
echo "You got an A!"
elif [ $GRADE -ge 80 ]; then
echo "You got a B!"
else
echo "Keep studying!"
fi
Looping with 'for'
Loops are essential for repeating tasks. The for loop iterates over a list of items or a range of numbers.
This is great for processing multiple files, user inputs, or performing actions a specific number of times.
#!/bin/bash
for FRUIT in Apple Banana Orange;
do
echo "I like $FRUIT."
done
# Another common way: sequence
for i in {1..3};
do
echo "Count: $i"
done
Looping with 'while'
The while loop continues to execute commands as long as a specified condition remains true. It's useful when you don't know exactly how many times you need to loop.
Be careful to include a way for the condition to eventually become false, or you'll create an infinite loop!
#!/bin/bash
COUNT=1
while [ $COUNT -le 3 ]; do
echo "The count is $COUNT."
COUNT=$((COUNT + 1))
done
Bash Scripting Check
What will be the output of this simple Bash script?
#!/bin/bash
NAME="Coddy"
AGE=5
if [ $AGE -gt 10 ]; then
echo "Hello, $NAME! You are a teen."
elif [ $AGE -eq 5 ]; then
echo "Hi, $NAME! You are young."
else
echo "Welcome, $NAME!"
fi
Recap: Bash Scripting Basics
Great job! You've learned the foundational elements of Bash scripting:
- Shebang:
#!/bin/bashto define the interpreter. - Executable: Use
chmod +xto run scripts. - Variables: Store data with
NAME=valueand access with$NAME. - Conditionals: Make decisions with
if,elif, andelseusing test operators like-gt,-eq. - Loops: Repeat tasks with
for(iterate lists/ranges) andwhile(loop until condition is false).
These basics are your building blocks for automating complex server tasks!
Frequently asked questions
Is the “Introduction to Bash Scripting” lesson free?
Yes — the full text of “Introduction to Bash Scripting” is free to read here on the web, and the Linux Server Deployment & SSH Mastery course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Linux Server Deployment & SSH Mastery course, upgrade to CoddyKit PRO.
What will I learn in “Introduction to Bash Scripting”?
Learn the fundamentals of Bash scripting, including variables, conditional statements, loops, and basic command execution. You practise Linux Server Deployment & SSH Mastery with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Linux Server Deployment & SSH Mastery?
No prior experience is required. Linux Server Deployment & SSH Mastery on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Introduction to Bash Scripting” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Linux Server Deployment & SSH Mastery lesson?
Yes. Every Linux Server Deployment & SSH Mastery lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Bash Scripting
- Automating Server Tasks
- Error Handling and Logging in Scripts
- Functions, Arguments, and Reusable Scripts