Skip to content

Lesson 11 — Linux Shell Scripting

Imagine you’re responsible for managing 500 Linux servers.

Every morning you need to:

  • Check disk usage
  • Verify system uptime
  • Restart failed services
  • Create user accounts
  • Backup important files
  • Generate health reports

Doing this manually would take hours.

Instead, Linux administrators automate these tasks using Shell Scripts.

Shell scripting is one of the most valuable skills for Linux Administrators, Cloud Engineers, DevOps Engineers, Site Reliability Engineers (SREs), and Cybersecurity Professionals.

Whether you’re deploying cloud infrastructure, configuring Kubernetes clusters, automating backups, or responding to security incidents, shell scripting saves time and reduces human error.


After completing this lesson, you will be able to:

  • Understand what shell scripting is.
  • Write and execute Bash scripts.
  • Use variables.
  • Accept user input.
  • Work with conditional statements.
  • Use loops and functions.
  • Automate common Linux administration tasks.
  • Apply shell scripting best practices.

A Shell Script is a text file containing a sequence of Linux commands that are executed automatically.

Instead of typing commands individually:

Terminal window
pwd
ls
date
whoami

You can place them inside a script:

#!/bin/bash
pwd
ls
date
whoami

Run once.

Everything executes automatically.


Shell scripting helps administrators:

  • Automate repetitive tasks
  • Reduce manual work
  • Improve consistency
  • Save time
  • Reduce configuration errors
  • Increase productivity

Automation is one of the core principles of modern IT operations.


Bash (Bourne Again SHell) is the default command-line shell for most Linux distributions.

It provides:

  • Command execution
  • Variables
  • Loops
  • Functions
  • Conditional statements
  • Input and output redirection

Most Linux automation starts with Bash.


Create a file.

Terminal window
nano hello.sh

Add the following:

#!/bin/bash
echo "Welcome to GoHackersCloud Academy!"

Save the file.


Grant execute permission.

Terminal window
chmod +x hello.sh

Execute the script.

Terminal window
./hello.sh

Output:

Welcome to GoHackersCloud Academy!

Every Bash script begins with:

#!/bin/bash

This tells Linux which interpreter should execute the script.


Comments explain your code.

Example:

Terminal window
# Display system information

Linux ignores comments during execution.


Variables store information.

Example:

#!/bin/bash
name="Student"
echo $name

Output:

Student

Variables make scripts reusable and dynamic.


Accept input from the keyboard.

#!/bin/bash
echo "Enter your name:"
read name
echo "Welcome $name"

Output:

Enter your name:
Rohit
Welcome Rohit

Linux provides built-in variables.

Examples:

Terminal window
echo $HOME
echo $USER
echo $PATH
echo $HOSTNAME

These variables provide information about the current environment.


Use if statements to make decisions.

Example:

#!/bin/bash
if [ -f test.txt ]
then
echo "File Exists"
fi

The script executes only if the condition is true.


#!/bin/bash
read age
if [ $age -ge 18 ]
then
echo "Eligible"
else
echo "Not Eligible"
fi

Operator Meaning
-eq Equal
-ne Not Equal
-gt Greater Than
-lt Less Than
-ge Greater Than or Equal
-le Less Than or Equal

These operators are commonly used in conditions.


Repeat a task multiple times.

Example:

Terminal window
for i in {1..5}
do
echo $i
done

Output:

1
2
3
4
5

Execute while a condition remains true.

Terminal window
count=1
while [ $count -le 5 ]
do
echo $count
count=$((count+1))
done

Functions organize reusable code.

#!/bin/bash
greet()
{
echo "Welcome!"
}
greet

Functions make scripts easier to maintain.


Scripts can accept parameters.

Example:

#!/bin/bash
echo "Hello $1"

Run:

Terminal window
./hello.sh Rohit

Output:

Hello Rohit

Every Linux command returns an exit status.

View the previous command’s exit code.

Terminal window
echo $?

Common values:

Code Meaning
0 Success
Non-Zero Error

Exit codes are important when automating workflows.


Save command output.

Terminal window
ls > files.txt

Append output.

Terminal window
date >> logfile.txt

Redirect errors.

Terminal window
command 2> error.log

Linux uses cron to execute scripts automatically.

Example:

Every day
2:00 AM
Backup Script

Cron automation is widely used in enterprise environments.


Practical Example 1 — System Information

Section titled “Practical Example 1 — System Information”
#!/bin/bash
echo "Hostname:"
hostname
echo "Current User:"
whoami
echo "Kernel:"
uname -r
echo "Disk Usage:"
df -h

#!/bin/bash
cp -r /home/student/Documents /backup

Automates file backups.


#!/bin/bash
echo "CPU"
top -bn1 | head
echo "Memory"
free -h
echo "Disk"
df -h

Useful for server monitoring.


Cloud Engineers automate:

  • EC2 provisioning
  • Software installation
  • User creation
  • Server configuration
  • Application deployment
  • Monitoring
  • Backups

Cloud-init scripts also use Bash during server startup.


DevOps Engineers use shell scripts for:

  • CI/CD Pipelines
  • Docker Builds
  • Kubernetes Deployments
  • Git Automation
  • Infrastructure Provisioning
  • Server Configuration

Bash remains a fundamental automation language.


Security professionals automate:

  • Log Analysis
  • File Integrity Checks
  • User Auditing
  • Vulnerability Scanning
  • Malware Detection
  • Incident Response

Automation significantly improves investigation speed.


Create script:

Terminal window
nano script.sh

Make executable:

Terminal window
chmod +x script.sh

Run script:

Terminal window
./script.sh

Check syntax:

Terminal window
bash -n script.sh

Display exit status:

Terminal window
echo $?

A Linux administrator manages 200 cloud servers.

Instead of manually checking each server, a Bash script:

Connect to Server
Check Disk Usage
Verify Running Services
Collect Logs
Generate Report
Email Results

This automation reduces hours of manual work to just a few minutes.


As a Linux administrator:

  • Write clear and readable scripts.
  • Use meaningful variable names.
  • Add comments to explain complex logic.
  • Test scripts in a lab before production.
  • Handle errors using exit codes.
  • Avoid hardcoding passwords or secrets.
  • Store scripts in version control systems like Git.
  • Follow consistent coding standards.

Well-written scripts improve reliability, maintainability, and security.


After completing this lesson, you should understand:

  • What shell scripting is.
  • How to create and execute Bash scripts.
  • Variables and user input.
  • Conditional statements.
  • Loops and functions.
  • Command-line arguments.
  • Exit codes and output redirection.
  • Enterprise scripting best practices.

Shell scripting is one of the most powerful skills in Linux administration.

By automating repetitive tasks, Bash scripts improve efficiency, consistency, and reliability across cloud, DevOps, cybersecurity, and enterprise environments.

Throughout your career, you’ll use shell scripting to deploy infrastructure, configure servers, automate monitoring, manage users, perform backups, and respond to incidents.

Mastering Bash scripting is a major step toward becoming an efficient Linux Administrator, Cloud Engineer, DevOps Engineer, or Cybersecurity Professional.


➡️ Lesson 12 — Linux Security Best Practices

In the next lesson, you’ll learn how to secure Linux systems by implementing user hardening, SSH security, firewall configuration, log monitoring, software updates, auditing, and enterprise security best practices.