Bash Basics¶
Bash (Bourne Again SHell) is the most widely used shell in Linux. It allows you to interact with the operating system, automate repetitive tasks, and write powerful shell scripts. Every Linux Administrator, DevOps Engineer, Cloud Engineer, and SRE should master Bash.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand what Bash is
- Execute commands in Bash
- Understand Bash syntax
- Use variables
- Understand quoting
- Use command substitution
- Work with environment variables
- Customize your Bash prompt
- Learn Bash best practices
Prerequisites¶
Before starting this lesson, you should complete:
- Module 1 – Linux Fundamentals
- Understanding the Shell
What is Bash?¶
Bash stands for:
Bourne Again SHell
It is the default shell on most Linux distributions.
Bash acts as a command interpreter between the user and the Linux kernel.
Whenever you type a command:
Bash interprets the command and asks the Linux kernel to execute it.
Why Bash is Important¶
Bash is everywhere.
It is used for:
- Linux Administration
- DevOps Automation
- Cloud Infrastructure
- Kubernetes Management
- CI/CD Pipelines
- Docker Automation
- Server Maintenance
If you know Bash, you can automate almost every Linux task.
Your First Bash Commands¶
Display current user:
Display current directory:
List files:
Create directory:
Delete directory:
Display date:
Every command follows this pattern:
Example:
| Part | Description |
|---|---|
| ls | Command |
| -la | Options |
| /home | Argument |
Running Multiple Commands¶
Commands can be chained together.
Run one after another:
Run on one line:
Meaning:
- Execute the next command only if the previous one succeeds.
Using semicolon:
Meaning:
- Execute every command regardless of success or failure.
Bash Variables¶
Variables store values.
Create a variable:
Display:
Output:
Another example:
Rules for Variable Names¶
Valid:
Invalid:
Built-in Environment Variables¶
Display current user:
Home directory:
Current shell:
Current working directory:
Hostname:
PATH variable:
Understanding PATH¶
When you type:
Bash searches for the executable inside directories listed in the PATH variable.
View PATH:
Example:
Search process:
Quoting in Bash¶
Bash supports three types of quotes.
Double Quotes¶
Variables are expanded.
Output:
Single Quotes¶
Variables are not expanded.
Output:
No Quotes¶
Output:
Command Substitution¶
Store command output inside a variable.
Old style:
Recommended:
Display:
Example:
Command History¶
Display previous commands:
Run a previous command:
Run previous command:
Search history:
Auto Completion¶
Press:
Example:
Automatically becomes:
This reduces typing mistakes.
Aliases¶
Aliases create shortcuts.
Example:
Now:
works like:
View aliases:
Remove:
Customizing the Prompt¶
Current prompt:
Temporarily change:
Output:
Useful for demonstrations and scripting.
Exit Status¶
Every Linux command returns an exit code.
Display it:
Common values:
| Exit Code | Meaning |
|---|---|
| 0 | Success |
| Non-zero | Failure |
Example:
Output:
Production Perspective¶
Bash powers many production workflows.
Examples:
kubectl get pods
docker ps
git pull
terraform apply
ansible-playbook site.yml
systemctl restart nginx
Almost every DevOps automation begins with Bash.
Hands-on Lab¶
Task 1¶
Display:
Task 2¶
Create variables.
Task 3¶
Display PATH.
Task 4¶
Practice command substitution.
Task 5¶
Create alias.
Task 6¶
Check exit code.
Mini Challenge¶
Without referring to the lesson:
Create variables:
- Your Name
- Company
- City
Print:
Use:
- Variables
- Command substitution
- echo
Best Practices¶
- Use meaningful variable names.
- Prefer
$(command)over backticks. - Use aliases for frequently used commands.
- Understand exit codes before scripting.
- Learn keyboard shortcuts to improve productivity.
Common Mistakes¶
❌ Forgetting $ while reading variables.
✅ Use:
Outputs:
Correct:
❌ Using spaces around =.
✅ Incorrect:
Correct:
❌ Using single quotes when variable expansion is required.
✅ Incorrect:
Correct:
Interview Questions¶
Beginner¶
- What is Bash?
- What does Bash stand for?
- How do you create a variable?
- What is PATH?
- What is command substitution?
Intermediate¶
- Explain Bash command execution.
- Difference between single and double quotes.
- What is an alias?
- Explain exit codes.
- Why is PATH important?
Architect Level¶
- Why is Bash still relevant in Cloud Computing?
- How does Bash improve DevOps automation?
- Why should engineers understand shell environment variables?
Summary¶
In this lesson, you learned:
- What Bash is
- Running commands
- Variables
- Environment variables
- PATH
- Quoting
- Command substitution
- Aliases
- Exit codes
- Bash productivity tips
These concepts form the foundation for Bash scripting, Linux administration, and DevOps automation.
Key Takeaways¶
- Bash is the default shell on most Linux systems.
- Variables store values and make scripts reusable.
- Environment variables configure your shell environment.
- PATH determines where Bash looks for executables.
- Command substitution allows commands to be embedded inside other commands.
- Understanding exit codes is essential for writing reliable automation.
What's Next?¶
In the next lesson, you'll learn:
- Absolute vs Relative Paths
cdpwdls- Directory navigation
- Hidden files
- Tab completion
- Efficient filesystem navigation techniques