Bash Scripting Tutorial for Beginners: Learn Bash Shell with Practical Examples (2026)
Bash (Bourne Again SHell) is one of the most widely used command-line shells and scripting languages for Linux, macOS, and Unix-based operating systems. It enables users to automate repetitive tasks, manage files, administer servers, and create powerful shell scripts.
Whether you’re a Linux beginner, DevOps engineer, system administrator, or software developer, learning Bash is an essential skill in 2026.
What is Bash?
Bash (Bourne Again SHell) is a Unix shell and command language developed as a free replacement for the Bourne Shell (sh). It serves as both an interactive command-line interpreter and a scripting language.
Bash allows users to:
- Execute Linux commands
- Automate tasks
- Write shell scripts
- Manage files and directories
- Control processes
- Schedule jobs
- Configure servers
History of Bash
- 1989 – Bash was developed by Brian Fox for the GNU Project.
- 1990s – Became the default shell on many Linux distributions.
- 2000s – Widely adopted by Unix and Linux administrators.
- Today – One of the most popular shell environments for Linux, cloud, and DevOps.
Why Learn Bash?
Bash remains an essential tool for developers and system administrators.
Benefits
- Easy to learn
- Automates repetitive tasks
- Saves time
- Installed on most Linux systems
- Excellent for server administration
- Essential for DevOps
- Works with Docker and Kubernetes
- Useful for CI/CD automation
Features of Bash
- Command-line interface
- Shell scripting
- Variables
- Loops
- Functions
- Conditional statements
- Process management
- File operations
- Input and output redirection
- Command substitution
Applications of Bash
Bash is commonly used for:
- Linux administration
- Server automation
- Backup scripts
- Deployment automation
- Log analysis
- File management
- Cron jobs
- DevOps workflows
- Docker automation
- CI/CD pipelines
Installing Bash
Linux
Bash is installed by default on most Linux distributions.
Check the version:
bash --version
macOS
Bash is included with macOS, although newer versions may require installation through Homebrew.
Windows
You can use Bash with:
- Windows Subsystem for Linux (WSL)
- Git Bash
- Cygwin
- MSYS2
Your First Bash Script
Create a file named hello.sh.
#!/bin/bash
echo "Hello, World!"
Save the file and make it executable.
chmod +x hello.sh
Run it.
./hello.sh
Output
Hello, World!
Understanding the Script
#!/bin/bash
This is called the Shebang. It tells the operating system which interpreter should execute the script.
echo
Prints text to the terminal.
Variables
name="Vikash"
echo $name
Output
Vikash
Read User Input
#!/bin/bash
echo "Enter your name:"
read name
echo "Welcome $name"
Comments
Single-line comment
# This is a comment
Arithmetic Operations
a=20
b=10
echo $((a+b))
echo $((a-b))
echo $((a*b))
echo $((a/b))
echo $((a%b))
If Statement
age=20
if [ $age -ge 18 ]
then
echo "Adult"
fi
If-Else
number=5
if [ $((number % 2)) -eq 0 ]
then
echo "Even"
else
echo "Odd"
fi
Nested If
marks=85
if [ $marks -ge 90 ]
then
echo "Grade A+"
elif [ $marks -ge 75 ]
then
echo "Grade A"
else
echo "Pass"
fi
Case Statement
fruit="Apple"
case $fruit in
Apple)
echo "Red Fruit"
;;
Banana)
echo "Yellow Fruit"
;;
*)
echo "Unknown"
;;
esac
For Loop
for i in {1..5}
do
echo $i
done
While Loop
count=1
while [ $count -le 5 ]
do
echo $count
((count++))
done
Until Loop
count=1
until [ $count -gt 5 ]
do
echo $count
((count++))
done
Functions
greet() {
echo "Hello $1"
}
greet "Vikash"
Output
Hello Vikash
Command Line Arguments
#!/bin/bash
echo "First Argument: $1"
echo "Second Argument: $2"
Run
./script.sh Hello Bash
Output
First Argument: Hello
Second Argument: Bash
Arrays
fruits=("Apple" "Mango" "Orange")
echo ${fruits[0]}
echo ${fruits[1]}
String Operations
text="GuruGyaan"
echo ${#text}
Length of string.
File Operations
Check file exists
if [ -f file.txt ]
then
echo "File exists"
fi
Check directory
if [ -d myfolder ]
then
echo "Directory exists"
fi
Reading a File
while read line
do
echo $line
done < file.txt
Writing to a File
echo "Hello Bash" > output.txt
Append
echo "New Line" >> output.txt
Redirect Output
ls > files.txt
Redirect errors
command 2> error.log
Redirect both output and errors
command > output.log 2>&1
Pipes
ls -l | grep ".txt"
Common Linux Commands
| Command | Description |
|---|---|
pwd | Print current directory |
ls | List files |
cd | Change directory |
mkdir | Create directory |
rm | Remove files |
cp | Copy files |
mv | Move or rename files |
cat | Display file contents |
touch | Create an empty file |
find | Search for files |
grep | Search text patterns |
chmod | Change file permissions |
chown | Change file ownership |
tar | Archive files |
zip | Compress files |
Environment Variables
Display PATH
echo $PATH
Create a variable
export APP_ENV=production
Cron Jobs
Edit cron jobs
crontab -e
Example
0 2 * * * /home/user/backup.sh
Runs the script every day at 2:00 AM.
Exit Status
echo $?
0= Success- Non-zero = Error
Bash Best Practices
- Use meaningful variable names.
- Quote variables when appropriate (
"$variable"). - Check command exit statuses.
- Use functions for reusable logic.
- Add comments for complex scripts.
- Handle errors gracefully.
- Avoid running scripts as root unless necessary.
- Validate user input.
- Keep scripts modular and readable.
- Test scripts before using them in production.
Advantages of Bash
- Free and open source
- Installed on most Linux systems
- Excellent automation capabilities
- Lightweight and fast
- Great for system administration
- Integrates with Linux utilities
- Powerful scripting features
- Ideal for DevOps workflows
Limitations of Bash
- Less suitable for large-scale software projects.
- Syntax can be difficult for beginners.
- Limited support for advanced data structures.
- Platform differences may affect script portability.
Career Opportunities
Knowledge of Bash is valuable for roles such as:
- Linux System Administrator
- DevOps Engineer
- Site Reliability Engineer (SRE)
- Cloud Engineer
- Backend Developer
- Cybersecurity Analyst
- Network Administrator
- Automation Engineer
Frequently Asked Questions (FAQs)
What is Bash?
Bash is a Unix shell and scripting language used to interact with the operating system and automate tasks.
Is Bash a programming language?
Bash is primarily a shell scripting language. It is designed for automation and command execution rather than general-purpose application development.
Is Bash only for Linux?
No. Bash is available on Linux, macOS, and Windows through tools such as WSL and Git Bash.
Is Bash difficult to learn?
No. Basic Bash commands and scripting are beginner-friendly, and more advanced topics can be learned gradually with practice.
Should I learn Bash before Python?
If you work with Linux or DevOps, learning basic Bash first is helpful. Python complements Bash and is better suited for complex automation tasks.
Conclusion
Bash is a powerful and practical scripting language for Linux and Unix environments. It simplifies system administration, automates repetitive tasks, and forms the backbone of many DevOps and cloud workflows. By learning Bash fundamentals—such as variables, loops, functions, file operations, and process management—you’ll be well-equipped to work efficiently with Linux systems and automate everyday tasks.
External Link Suggestions:
- GNU Bash Manual: https://www.gnu.org/software/bash/manual/
- Bash Reference Manual: https://www.gnu.org/software/bash/manual/bash.html
- Linux Documentation Project: https://tldp.org/
- Bash Guide for Beginners: https://tldp.org/LDP/Bash-Beginners-Guide/html/