Bash and Shell Scripting
Turn ten repeated commands into one β the foundation of every DevOps automation.
Imagine repeating the same ten commands every morning. Now imagine typing one command and letting the computer do all the work. Thatβs exactly what Bash is for. Bash is the default shell on most Linux systems and one of the most important tools a DevOps engineer must master.
π― Learning Objectives
By the end of this lesson you will:
- Understand what Bash is
- Execute commands and use variables
- Display text with
echo - Create, make executable, and run your first script
- Read user input
What is Bash?
Bash stands for Bourne Again SHell. A shell is a program that lets you communicate with Linux β instead of clicking buttons, you type commands.
You
β
Bash (the shell)
β
Linux Kernel
β
ComputerAlmost every DevOps automation begins with Bash.
Your First Command and Variables
Variables store information. The $ tells Bash to use the value stored inside:
name="John"
echo $name # John
echo $HOME # /home/student (built-in)
echo $USER # student (built-in)Creating Your First Script
Create a file, open it in an editor, and add a shebang plus a command:
nano hello.sh#!/bin/bash
echo "Welcome to DevOps!"Save it, make it executable, and run it:
Congratulations β youβve written your first Bash program.
β Two things beginners forget
- The shebang
#!/bin/bashon line 1 β it tells Linux how to run the file. - The execute permission β if you see
Permission denied, runchmod +x filename.sh.
Reading User Input
#!/bin/bash
echo "What is your name?"
read username
echo "Welcome $username!"Anything after # is a comment and is ignored by Bash β use comments to explain your code.
Real DevOps Example
Instead of typing three deploy commands every day, put them in a script and run it with one command:
#!/bin/bash
# deploy.sh β build and ship the app
git pull
docker build -t app .
docker compose up -dAutomation saves time and removes human error.
π§ͺ Hands-on Lab
Build a system-info Script
Create a script named system-info.sh that prints:
- The current user
- The current directory
- Todayβs date
- Your home directory
Try it yourself before revealing the solution.
π‘ DevOps Tip
Whenever you catch yourself repeating commands, ask: βCan I automate this with Bash?β Very often the answer is yes.
π§ Knowledge Check
Which command prints text to the screen?
What must the FIRST line of a Bash script be?
πΌ Interview Preparation
What is a shebang, and what happens if you omit it?
Summary
Youβve taken your first step into Linux automation. Every modern DevOps pipeline relies on scripts to automate deployments, backups, and monitoring.