← Back to Home
πŸ“œ

Bash and Shell Scripting

Turn ten repeated commands into one β€” the foundation of every DevOps automation.

⏱14 min readπŸ“šDevOps Fundamentals

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:


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.

text
You
↓
Bash (the shell)
↓
Linux Kernel
↓
Computer

Almost every DevOps automation begins with Bash.


Your First Command and Variables

bash β€” 80Γ—24
student@devops:~$echo "Hello Linux!"

Variables store information. The $ tells Bash to use the value stored inside:

bash
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:

bash
nano hello.sh
bash
#!/bin/bash

echo "Welcome to DevOps!"

Save it, make it executable, and run it:

bash β€” 80Γ—24
student@devops:~$chmod +x hello.sh && ./hello.sh

Congratulations β€” you’ve written your first Bash program.

⚠ Two things beginners forget


Reading User Input

bash
#!/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:

bash
#!/bin/bash
# deploy.sh β€” build and ship the app

git pull
docker build -t app .
docker compose up -d
bash β€” 80Γ—24
student@devops:~$./deploy.sh

Automation saves time and removes human error.


πŸ§ͺ Hands-on Lab

πŸ“

Build a system-info Script

Create a script named system-info.sh that prints:

  1. The current user
  2. The current directory
  3. Today’s date
  4. 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

Knowledge Check

Which command prints text to the screen?

Knowledge Check

What must the FIRST line of a Bash script be?


πŸ’Ό Interview Preparation

Interview Q&A

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.

Up Next

Linux Networking

You've mastered this lesson. Continue your journey to becoming a DevOps Engineer.

Start Next Lesson→