Shell Scripting Made EASY

Israel AdeyemoIsrael Adeyemo
5 min read

🐚 What is Shell Scripting?


💬 Definition

Shell scripting is the practice of writing a series of commands for the shell (command-line interpreter) to execute automatically. These commands are written in a plain text file, which is referred to as a shell script.


📦 What is a Shell?

A shell is a program that provides a text-based interface for users to interact with the operating system.

Popular shells include:

ShellDescription
shBourne Shell (original UNIX shell)
bashBourne Again SHell (most common, Linux default)
zshZ Shell (more advanced features)
cshC Shell (uses C-like syntax)
fishFriendly Interactive Shell (user-friendly design)

📜 What is a Shell Script?

A shell script is a file that contains a sequence of shell commands that are executed in order.

Example:

bash

Copy

#!/bin/bash

echo "Starting backup..."

tar -czf backup.tar.gz /home/user

echo "Backup complete."

🔍 What’s Happening Here?

 #!/bin/bash: Tells the system to use the Bash shell.

echo: Prints messages to the screen.

tar: Compresses the /home/user directory into a .tar.gz file.


🎯 Why Use Shell Scripting?

Shell scripting is used to:

● ✅ Automate repetitive tasks (backups, updates, deployments)

● ✅ Configure system settings

● ✅ Run multiple commands as a single program

● ✅ Schedule jobs with tools like cron

● ✅ Quickly test logic or prototypes


🛠️ Real-Life Examples

TaskHow Shell Script Helps
Daily backupsAutomatically compress important files at night
Server setupInstall packages, configure services, start apps
Log analysisParse and summarize log files
DeploymentPull code, build Docker image, start containers
CI/CD pipelineRun tests, build apps, deploy to servers

🧠 Key Characteristics of Shell Scripts

Plain text files (.sh)

● Run using bash scriptname.sh or ./scriptname.sh

● Start with a shebang (#!/bin/bash) to specify the interpreter

● Can use:

○ **Variables

Conditions* (if, else)*

Loops (for, while)

○ **Functions

Command substitution* ($(command))*


🧪 Example: Simple Script

bash

Copy

#!/bin/bash

echo "Welcome to Shell Scripting"

date

uptime

This script:

  1. Greets the user
  1. Shows the current date
  1. Displays how long the system has been running

💡 Summary

FeaturePurpose
ShellInterface to run OS commands
ScriptFile with commands for automation
BashMost commonly used shell in Linux/DevOps
ScriptingSaves time, reduces manual errors, boosts efficiency

🧠 Introduction to Bash Syntax

(For Shell Scripting Beginners)


🧾 1. Variables in Bash

Variables are used to store data for reuse. In Bash, you don’t need to declare a type (everything is treated as a string unless used in arithmetic).

🔧 Syntax

bash

Copy

variable_name=value

⚠️ No spaces before or after the equal sign.

🧪 Example

bash

Copy

name="DevOps Student"

echo "Welcome, $name!"

🔍 Output

Copy

Welcome, DevOps Student!

📥 2. User Input

Use the read command to take input from the user.

🧪 Example

bash

Copy

#!/bin/bash

read -p "Enter your name: " username

echo "Hello, $username!"

🔍 What’s Happening?

 read -p: Prompts the user and stores input in a variable.

 $username: Prints what the user entered.


📤 3. Output (echo)

The echo command displays text or variables.

🧪 Example

bash

Copy

echo "Welcome to Bash scripting"

You can also format text:

bash

Copy

echo -e "Line 1\nLine 2"


❓ 4. Conditional Statements

Bash uses if, then, else, and fi to control decision-making.

🧪 Example

bash

Copy

#!/bin/bash

read -p "Enter username: " user

if [ "$user" == "admin" ]; then

  echo "Access granted"

else

echo "Access denied"

fi

🔍 Breakdown:

[ "$user" == "admin" ]: Checks if user is "admin"

 then: Executes block if true

else: Executes if false

fi: Ends the if statement


🔁 5. Loops

Loops help automate repetitive tasks.

🔁 For Loop

bash

Copy

for i in {1..5}

do

  echo "Iteration $i"

done

🔁 While Loop

bash

Copy

count=1

while [ $count -le 3 ]

do

  echo "Count is $count"

  ((count++))

done


🧩 6. Functions

Functions allow you to reuse blocks of code.

🧪 Example

bash

Copy

greet() {

  echo "Hello, $1"

}

 greet "DevOps Engineer"

$1 is the first argument to the function.


⚙️ 7. Command Substitution

You can run a command and use its output in a variable.

🧪 Example

bash

Copy

current_date=$(date)

echo "Today is $current_date"


🛡️ 8. Exit Status ($?)

Every command returns a status code: 0 = success, non-zero = failure.

🧪 Example

bash

Copy

ls /some/folder

echo "Exit status was $?"


✅ Summary Table

ConceptSyntaxExample
Variablex=5echo $x
Inputread varread -p "Enter:" name
Outputechoecho "Hi"
Conditionif ... fiif [ $x -eq 5 ]; then
Loopfor/whilefor i in {1..5}
Functionfunc() {}greet John
Command Sub.$(command)$(date)

🧠 Your First Task

Write a script that:

  1. Asks for the user’s name
  1. Greets them
  1. Shows the current date and uptime

Example structure:

bash

Copy

#!/bin/bash

read -p "Enter your name: " name

echo "Hi, $name!"

echo "Today is: $(date)"

echo "System uptime:"

uptime

See the outcome result below

0
Subscribe to my newsletter

Read articles from Israel Adeyemo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Israel Adeyemo
Israel Adeyemo