A shell script is a text file that contains a series of Linux/Unix commands. Instead of typing commands one by one in the terminal, you write them in a file and run it.
- Manual work → typing commands every time
- Shell script → automate that work with one command
Shell scripts are commonly used for:
- Automation (backups, cleanup)
- System administration
- File handling
- Monitoring system resources
A shell is a program that:
- Takes commands from the user
- Sends them to the operating system
- Shows the output
Common shells:
bash(most popular)shzshfish
In most Linux systems, Bash (Bourne Again Shell) is the default.
This line is called a shebang.
#!/bin/bash- It tells the system which shell should execute the script
- Without it, the system may not know how to run the file
#!→ special characters (shebang)/bin/bash→ path to the Bash shell
Meaning:
“Run this script using Bash”
If your script starts with:
#!/bin/bashThe OS knows: Use Bash to interpret the commands inside the file.
nano hello.sh#!/bin/bash
echo "Hello, World!"
echo "Welcome to Shell Scripting"chmod +x hello.sh./hello.sh#!/bin/bashUses Bash shell
echo "Hello, World!"echo prints text on the screen
echo "Welcome to Shell Scripting"Prints another message
#!/bin/bash
echo "Enter your name:"
read name
echo "Hello $name, welcome to Shell Scripting!"read name→ takes input from user$name→ accesses the variable- Variables do NOT need a data type
#!/bin/bash
echo "Enter a number:"
read num
if [ $num -gt 10 ]
then
echo "Number is greater than 10"
else
echo "Number is 10 or less"
fiif,then,else,fi→ conditional statements-gt→ greater than[ ]→ test condition
File Organizer Script
- Create folders
- Move files based on extension
- Automate file organization
#!/bin/bash
# Create directories
mkdir -p Images Documents Scripts
# Move files based on extension
mv *.jpg *.png Images 2>/dev/null
mv *.pdf *.txt Documents 2>/dev/null
mv *.sh Scripts 2>/dev/null
echo "Files organized successfully!"mkdir -p Images Documents ScriptsCreates folders (no error if already exists)
mv *.jpg *.png ImagesMoves image files into Images folder
2>/dev/nullHides error messages if no files found
echo "Files organized successfully!"Prints success message
#!/bin/bash
# -------- Variables --------
USERNAME=$1
ACTION=$2
# -------- Function: show usage --------
usage() {
echo "Usage: $0 <username> <action>"
echo "Actions:"
echo " check - Check if user exists"
echo " info - Show user ID info"
}
# -------- Function: check user --------
check_user() {
if id "$USERNAME" &>/dev/null
then
echo "User '$USERNAME' exists"
else
echo "User '$USERNAME' does NOT exist"
fi
}
# -------- Function: user info --------
user_info() {
if id "$USERNAME" &>/dev/null
then
echo "User information:"
id "$USERNAME"
else
echo "Cannot show info. User does not exist"
fi
}
# -------- Argument validation --------
if [ $# -ne 2 ]
then
usage
exit 1
fi
# -------- Action handler --------
if [ "$ACTION" == "check" ]
then
check_user
elif [ "$ACTION" == "info" ]
then
user_info
else
echo "Invalid action"
usage
fi