Shell scripting is one of the most powerful features of Linux and Unix-like operating systems. A shell script is a text file containing a series of commands that are executed by the shell. By combining commands into scripts, users can automate repetitive tasks, manage systems more efficiently, and create powerful administrative tools.
This tutorial introduces the fundamentals of shell scripting using the Bash shell, the most common shell on Linux systems.
What Is a Shell Script?
A shell script is a plain text file containing Linux commands that are executed sequentially by a shell interpreter.
Instead of typing the same commands repeatedly, you can place them into a script and run them whenever needed.
For example, instead of typing:
date
pwd
ls
every day, you could place these commands into a script and run them with a single command.
Creating Your First Script
Create a new file:
nano hello.sh
Add the following content:
#!/bin/bash
echo "Hello, World!"
Save the file.
The first line is called the shebang:
#!/bin/bash
It tells Linux which interpreter should execute the script.
Making the Script Executable
Before running the script, make it executable:
chmod +x hello.sh
Run the script:
./hello.sh
Output:
Hello, World!
Using Variables
Variables store data that can be reused throughout a script.
Example:
#!/bin/bash
name="Ron"
echo "Hello, $name"
Output:
Hello, Ron
Notice there are no spaces around the equals sign.
Correct:
name="Ron"
Incorrect:
name = "Ron"
Getting User Input
The read command allows a script to accept user input.
#!/bin/bash
echo "What is your name?"
read name
echo "Hello, $name"
Example:
What is your name?
Ron
Hello, Ron
Using Command Substitution
Store command output in a variable:
#!/bin/bash
today=$(date)
echo "Today's date is:"
echo "$today"
Output:
Today's date is:
Mon Jun 16 12:00:00 EDT 2026
Working with Arguments
Scripts can accept command-line arguments.
Example:
#!/bin/bash
echo "Hello, $1"
Run:
./hello.sh Ron
Output:
Hello, Ron
Special variables:
| Variable | Description |
|---|---|
| $0 | Script name |
| $1 | First argument |
| $2 | Second argument |
| $# | Number of arguments |
| $@ | All arguments |
Using If Statements
Conditional statements allow scripts to make decisions.
Example:
#!/bin/bash
number=10
if [ $number -gt 5 ]
then
echo "Number is greater than 5"
fi
Output:
Number is greater than 5
Common comparison operators:
| Operator | Meaning |
|---|---|
| -eq | Equal |
| -ne | Not equal |
| -gt | Greater than |
| -lt | Less than |
| -ge | Greater than or equal |
| -le | Less than or equal |
If-Else Statements
Example:
#!/bin/bash
read age
if [ $age -ge 18 ]
then
echo "Adult"
else
echo "Minor"
fi
Using Loops
For Loop
#!/bin/bash
for i in 1 2 3 4 5
do
echo $i
done
Output:
1
2
3
4
5
While Loop
#!/bin/bash
count=1
while [ $count -le 5 ]
do
echo $count
count=$((count + 1))
done
Output:
1
2
3
4
5
Creating Functions
Functions allow code reuse.
Example:
#!/bin/bash
greet() {
echo "Welcome to Linux"
}
greet
Output:
Welcome to Linux
Functions can be called multiple times throughout a script.
Working with Files
Check whether a file exists:
#!/bin/bash
if [ -f test.txt ]
then
echo "File exists"
else
echo "File not found"
fi
Common file tests:
| Test | Meaning |
|---|---|
| -f | Regular file exists |
| -d | Directory exists |
| -r | Readable |
| -w | Writable |
| -x | Executable |
Simple Backup Script
Example:
#!/bin/bash
cp important.txt important.txt.bak
echo "Backup completed."
This creates a backup copy of a file.
Displaying System Information
Example:
#!/bin/bash
echo "Hostname:"
hostname
echo ""
echo "Kernel Version:"
uname -r
echo ""
echo "Disk Usage:"
df -h
This script gathers useful system information.
Debugging Scripts
Run a script in debug mode:
bash -x script.sh
The shell displays each command as it executes.
Example:
+ echo Hello
Hello
Debugging is extremely useful when troubleshooting scripts.
Best Practices
When writing shell scripts:
- Use meaningful variable names.
- Add comments to explain complex code.
- Test scripts before using them on production systems.
- Always quote variables when appropriate.
- Check for errors and invalid input.
- Keep scripts simple and readable.
Example comment:
# Backup important files
cp file.txt file.txt.bak
Common Administrative Uses
System administrators frequently use shell scripts to:
- Create backups
- Monitor servers
- Manage users
- Rotate log files
- Automate software updates
- Generate reports
- Deploy applications
Because shell scripts can combine many Linux commands into a single automated task, they remain one of the most important tools in Linux administration.
Conclusion
Shell scripting allows Linux users to automate tasks, simplify administration, and create powerful tools using simple text files. By learning variables, input handling, loops, conditions, functions, and file operations, beginners can quickly begin writing useful scripts. As your skills grow, shell scripting can become a foundation for more advanced Linux system administration and DevOps work.