Kritim Yantra
May 04, 2025
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.
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.
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:
But for most beginners, Bash is the go-to shell.
Hereโs what a simple shell script looks like:
#!/bin/bash
# This is a comment
echo "Hello, world!"
#!/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.nano hello.sh
Paste this:
#!/bin/bash
echo "Welcome to Shell Scripting!"
Ctrl + X
, then Y
, then Enter
)chmod +x hello.sh
./hello.sh
And you'll see:
Welcome to Shell Scripting!
Variables store data like numbers or strings.
#!/bin/bash
name="John"
echo "Hello, $name!"
Output:
Hello, John!
=
$variable_name
to use the valueShell 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))"
#!/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 equalfor i in 1 2 3 4 5
do
echo "Number: $i"
done
count=1
while [ $count -le 5 ]
do
echo "Count: $count"
count=$((count + 1))
done
echo "What is your name?"
read username
echo "Hello, $username!"
filename="myfile.txt"
if [ -f $filename ]; then
echo "$filename exists"
else
echo "$filename does not exist"
fi
for file in *.txt
do
echo "Processing $file"
done
greet() {
echo "Hello, $1!"
}
greet "Alice"
Output:
Hello, Alice!
Every command returns an exit status:
0
means successYou can check it using $?
:
mkdir newfolder
if [ $? -eq 0 ]; then
echo "Directory created"
else
echo "Failed to create directory"
fi
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"
bash script.sh
before making them executableShell 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!
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google