Shell Scripting for Beginners: A Detailed Guide

Author

Kritim Yantra

May 04, 2025

Shell Scripting for Beginners: A Detailed Guide

Have you ever wondered how developers automate repetitive tasks or configure systems with just a few lines of code? That magic is often done with shell scripts! If you're new to the world of programming or just starting with Linux/Unix, this blog is the perfect place to begin your journey into shell scripting.


๐ŸŒŸ What is Shell Scripting?

Shell scripting is writing a series of commands for the shell (the command-line interpreter) to execute. These scripts are saved in a file (usually with a .sh extension) and can automate tasks like file backups, system monitoring, software installation, and more.

Think of it like this:

๐Ÿ’ฌ You give instructions to your computer in a list, and the computer executes them one by one โ€” thatโ€™s shell scripting.


๐Ÿ’ก What is a Shell?

A shell is a program that takes commands from the user and gives them to the operating system to perform. The most common shell is Bash (Bourne Again SHell), used in most Linux distributions and macOS.

Other popular shells include:

  • sh โ€“ Bourne shell
  • zsh โ€“ Z shell
  • ksh โ€“ Korn shell
  • fish โ€“ Friendly Interactive SHell

But for most beginners, Bash is the go-to shell.


๐Ÿงพ Why Learn Shell Scripting?

  • โœ… Automate repetitive tasks
  • โœ… Save time and reduce human error
  • โœ… Manage servers and environments
  • โœ… Build deployment scripts for projects
  • โœ… Itโ€™s a foundational skill for DevOps, Linux admins, and backend developers

๐Ÿ“ Basic Shell Script Structure

Hereโ€™s what a simple shell script looks like:

#!/bin/bash
# This is a comment

echo "Hello, world!"

๐Ÿ” Explanation:

  • #!/bin/bash: This is called a shebang. It tells the system to use the Bash shell to interpret this script.
  • # This is a comment: Anything after # is a comment and not executed.
  • echo "Hello, world!": This prints text to the screen.

๐Ÿ“Œ How to Create and Run a Shell Script

Step 1: Create a file

nano hello.sh

Paste this:

#!/bin/bash
echo "Welcome to Shell Scripting!"

Step 2: Save and exit (in nano: press Ctrl + X, then Y, then Enter)

Step 3: Give the script permission to execute

chmod +x hello.sh

Step 4: Run the script

./hello.sh

And you'll see:

Welcome to Shell Scripting!

๐Ÿ› ๏ธ Variables in Shell Scripts

Variables store data like numbers or strings.

#!/bin/bash
name="John"
echo "Hello, $name!"

Output:

Hello, John!

๐Ÿ”‘ Note:

  • No spaces around =
  • Use $variable_name to use the value

๐Ÿงฎ Arithmetic Operations

Shell supports simple arithmetic using expr or $(()):

a=10
b=5

# Using expr
result=$(expr $a + $b)
echo "Sum is $result"

# Using double parentheses
echo "Product is $(($a * $b))"

๐Ÿ” Conditional Statements

If-else

#!/bin/bash
num=10

if [ $num -gt 5 ]; then
  echo "$num is greater than 5"
else
  echo "$num is not greater than 5"
fi

Operators:

  • -eq : equal to
  • -ne : not equal
  • -gt : greater than
  • -lt : less than
  • -ge : greater or equal
  • -le : less or equal

๐Ÿ”„ Loops in Shell Scripts

For Loop

for i in 1 2 3 4 5
do
  echo "Number: $i"
done

While Loop

count=1
while [ $count -le 5 ]
do
  echo "Count: $count"
  count=$((count + 1))
done

๐Ÿ“ฅ Reading User Input

echo "What is your name?"
read username
echo "Hello, $username!"

๐Ÿ“‚ Working with Files

Check if file exists:

filename="myfile.txt"

if [ -f $filename ]; then
  echo "$filename exists"
else
  echo "$filename does not exist"
fi

Loop through files in a folder:

for file in *.txt
do
  echo "Processing $file"
done

๐Ÿ”ง Functions in Shell Scripts

greet() {
  echo "Hello, $1!"
}

greet "Alice"

Output:

Hello, Alice!

๐Ÿ’ฃ Error Handling (Exit Status)

Every command returns an exit status:

  • 0 means success
  • Anything else means an error

You can check it using $?:

mkdir newfolder
if [ $? -eq 0 ]; then
  echo "Directory created"
else
  echo "Failed to create directory"
fi

๐Ÿ“‹ Example: Backup Script

Letโ€™s create a real-world use case โ€” backing up a folder.

#!/bin/bash
# Simple backup script

backup_folder="/home/user/documents"
destination="/home/user/backup"

date=$(date +%F)
backup_file="backup-$date.tar.gz"

tar -czf $destination/$backup_file $backup_folder

echo "Backup completed: $backup_file"

๐Ÿ“š Tips for Beginners

  • Always test scripts with bash script.sh before making them executable
  • Use comments to explain your logic
  • Use shellcheck (a linting tool) to catch script errors
  • Learn by automating your own small tasks

๐Ÿ“˜ Final Thoughts

Shell scripting might look simple, but it's incredibly powerful. Whether you're managing servers, automating tasks, or just learning Linux, shell scripts can save you hours of work and make you a more efficient developer.

โœจ Start small, experiment, and keep scripting โ€” soon, you'll be automating like a pro!

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts