Bash Tutorial for Beginners and Pros | Learn Bash Scripting Step-by-Step 2026
Introduction to Bash
What is Bash?
Bash stands for Bourne Again SHell. It is a command-line interpreter that allows users to interact directly with the operating system. Think of it as a text-based way to tell your computer what to do β no clicking, just typing.
Why Use Bash?
Itβs fast, efficient, and perfect for automation. Whether you’re managing servers, writing scripts, or just exploring Linux, Bash is your go-to companion.
Who Uses Bash?
Everyone from beginners tinkering on their Raspberry Pi to DevOps engineers managing complex cloud systems.
Getting Started with Bash
Installing Bash
Most Linux and macOS systems already come with Bash. On Windows, you can interact with Unix-like environments using Git Bash or the Windows Subsystem for Linux (WSL).
Launching the Bash Shell
Just open your terminal. On Linux/Mac, type bash and press Enter.
Your First Bash Command
Type:
echo "Hello, world!"
Boom. Youβve just run your first Bash command.
Basic Bash Commands
Navigation Commands
pwd, cd, and ls
pwdβ shows the current directory.cd foldernameβ moves to a folder.lsβ lists files in the current directory.
File Operations
touch, cp, mv, and rm
touch file.txtβ creates a file.cp file.txt copy.txtβ copies a file.mv file.txt newname.txtβ renames or moves.rm file.txtβ deletes a file.
Viewing File Contents
cat, less, more, head, tail
cat file.txtβ prints content.less/moreβ view file page by page.head -n 5β first 5 lines.tail -n 5β last 5 lines.
Bash Scripting Basics
Writing Your First Bash Script
Create a file:
nano myscript.sh
Add:
#!/bin/bash
echo "This is a script!"
Making Scripts Executable
chmod +x myscript.sh
Script Structure and Comments
Use # for comments. Always start with #!/bin/bash at the top.
Variables and Data Types
Defining and Using Variables
name="John"
echo "Hi, $name"
Environment Variables
Check with env or printenv.
Quoting Variables
Always quote strings:
echo "$name"
Conditional Statements
if, elif, else
if [ $age -gt 18 ]; then
echo "Adult"
else
echo "Minor"
fi
case Statements
case $fruit in
apple) echo "Apple selected";;
*) echo "Unknown fruit";;
esac
Loops in Bash
for, while, until Loops
for i in {1..5}; do
echo $i
done
Loop Control: break and continue
Exit early with break, skip iterations with continue.
Functions in Bash
Defining and Calling Functions
say_hello() {
echo "Hello!"
}
say_hello
Function Arguments
greet() {
echo "Hello, $1"
}
greet "Alice"
Input and Output
Reading User Input
read -p "Enter name: " username
echo "Welcome $username"
Redirecting Input and Output
>overwrites>>appends<input redirection
Pipes and Filters
cat file.txt | grep "search"
Error Handling in Bash
Exit Status
Every command returns a code. 0 is success, anything else is an error.
Using trap for Cleanup
trap "echo 'Script interrupted'; exit" SIGINT
Working with Arrays
Declaring Arrays
fruits=("apple" "banana" "cherry")
Accessing and Iterating Arrays
echo "${fruits[1]}"
for f in "${fruits[@]}"; do echo $f; done
Advanced Scripting Concepts
Using getopts for Arguments
while getopts ":u:p:" opt; do
case $opt in
u) user=$OPTARG ;;
p) pass=$OPTARG ;;
esac
done
Regular Expressions with grep
grep -E "pattern" file.txt
Debugging Bash Scripts
set -x and set -e
set -x: prints each command.set -e: exits on error.
Using echo for Debugging
Sprinkle echo like breadcrumbs to trace issues.
Real-world Bash Script Examples
Backup Script
#!/bin/bash
tar -czf backup.tar.gz /home/user/docs
Automation Script
#!/bin/bash
for file in *.log; do
mv "$file" logs/
done
Best Practices and Tips
Writing Clean Code
- Indent consistently.
- Use meaningful variable names.
Security Considerations
- Never blindly run user input.
- Avoid
evalunless absolutely necessary.
Conclusion: Bash Tutorial
Bash might look intimidating at first, but once you get the hang of it, it’s like wielding a superpower. Whether you’re automating backups, parsing logs, or creating complex tools, Bash will save you time and make your workflows efficient. So open that terminal and start scripting!
FAQs: Bash Tutorial
1. Is Bash only for Linux?
No, itβs available on macOS, and even Windows through WSL or Git Bash.
2. How is Bash different from other shells like Zsh or Fish?
Bash is widely used and more standard, while Zsh and Fish offer more features and customization out-of-the-box.
3. Can I use Bash for web development?
Indirectly, yes. Itβs great for managing servers, automating deployment, or running build scripts.
4. What’s the best way to learn Bash?
Practice! Start with simple scripts, automate daily tasks, and build from there.
5. Are Bash scripts safe?
They can be, but always sanitize input and follow scripting best practices.
Share Now :
