π Bash Scripting Made Easy β Learn by Example (With Outputs!)

Bash scripting is a way to automate tasks in Linux using a series of shell commands written in a text file. Think of it as creating a to-do list for your computer to follow β automatically..
π 1. Getting Started with Bash Scripts
Every Bash script should start with this line:
bashCopyEdit#!/bin/bash
Why? This is called a shebang. It tells the computer: βUse the Bash shell to run this file.β
π§ͺ Example:
bashCopyEdit#!/bin/bash
echo -e "π Welcome to Bash Scripting!\nLet's start learning... π»"
echo
prints the message-e
enables special characters like\n
(new line) and\t
(tab)
β How to Run:
bashCopyEditchmod +x hello.sh # Make it executable
./hello.sh # Run the script
π€ Output:
vbnetCopyEditπ Welcome to Bash Scripting!
Let's start learning... π»
π‘ 2. Variables β Store and Use Data
π Static Variables
You can define your own variables to store information:
bashCopyEdit#!/bin/bash
name="Alice"
city="Paris"
company="OpenAI"
echo "Hi, I'm $name from $city, and I work at $company."
$name
pulls the value stored in the variableQuotes are not required but help with spacing
π€ Output:
vbnetCopyEditHi, I'm Alice from Paris, and I work at OpenAI.
π Dynamic Input
Let the user provide input at runtime:
bashCopyEdit#!/bin/bash
echo "Enter your name:"
read name
echo "Enter your city:"
read city
echo "Enter your company:"
read company
echo "Hi $name from $city! You work at $company. π"
read
waits for user input and stores it in the variable
π€ Output:
yamlCopyEditEnter your name:
Alice
Enter your city:
Paris
Enter your company:
OpenAI
Hi Alice from Paris! You work at OpenAI. π
π 3. Password Input β Hidden Typing
Sometimes we donβt want to show the password as someone types it.
bashCopyEdit#!/bin/bash
echo "Enter your username:"
read user
read -sp "Enter your password: " pass
echo -e "\nWelcome, $user! Your password is safely captured. π"
-s
makesread
silent (hides input)-p
adds a prompt message inline
π― 4. Script Arguments β Pass Data from Command Line
You can pass values when you run the script:
bashCopyEdit#!/bin/bash
echo "Name: $1"
echo "Country: $2"
echo "Company: $3"
echo "All arguments: $@"
echo "Script name: $0"
$1
,$2
, etc. refer to each input argument$@
means all arguments$0
is the script filename
β Run:
bashCopyEditsh info.sh Alice France OpenAI
β 5. If Conditions β Logic in Scripts
π Simple If Statement
bashCopyEditnum=10
if [[ $num -eq 10 ]]; then
echo "βοΈ The number is exactly 10"
fi
-eq
means βequal toβ (used for numbers)Double square brackets
[[ ]]
are safer than single[ ]
π If-Else
bashCopyEditnum=5
if [[ $num -eq 10 ]]; then
echo "Number is 10"
else
echo "β Number is not 10"
fi
else
runs only whenif
fails
π Nested If / Grading System
bashCopyEditecho "Enter your score:"
read score
if [[ $score -eq 100 ]]; then
echo "π First Prize!"
elif [[ $score -eq 50 ]]; then
echo "π₯ Second Prize"
elif [[ $score -eq 25 ]]; then
echo "π₯ Third Prize"
else
echo "π Better luck next time!"
fi
elif
lets you check multiple conditionselse
is the default if none match
π’ 6. Comparing Strings and Numbers
π String Comparison
bashCopyEditperson1="Alice"
person2="Alice"
if [[ $person1 == $person2 ]]; then
echo "β
Both names match"
fi
Use
==
for string comparisonUse
!=
for "not equal"
π Number Comparison
bashCopyEdita=20
b=10
if [[ $a -gt $b ]]; then
echo "πΌ a is greater than b"
fi
-gt
: greater than-lt
: less than-ge
: greater or equal-le
: less or equal-ne
: not equal-eq
: equal
π 7. Simple Login Script (AND / OR)
Using AND
(&&)
bashCopyEditread -p "Username: " user
read -sp "Password: " pass
if [[ $user == "admin" && $pass == "admin@123" ]]; then
echo -e "\nβ
Welcome, admin!"
else
echo -e "\nβ Invalid credentials"
fi
- Both username and password must match
Using OR
(||)
bashCopyEditif [[ $user == "admin" || $pass == "admin@123" ]]; then
echo -e "\nβοΈ At least one credential is correct"
else
echo -e "\nβ Neither is correct"
fi
π 8. File Checking
bashCopyEditfile="/home/user/data.txt"
if [[ -e $file ]]; then
echo "π File exists!"
else
echo "β File not found!"
fi
-e
checks if a file existsYou can also use
-f
for regular files and-d
for directories
π Final Thoughts
You now know how to:
Write basic scripts
Use variables and input
Pass arguments
Add logic (if/else)
Read passwords safely
Check for files
Subscribe to my newsletter
Read articles from Vinuthna directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Vinuthna
Vinuthna
Why Collaborate with Me? ~ Infrastructure as Code (IaC): Skilled in automating infrastructure provisioning with Terraform and Ansible, ensuring consistency, scalability, and repeatability. ~ Containerization & Orchestration: Expert in building, managing, and scaling containerized applications using Docker and Kubernetes clusters. ~ CI/CD Expertise: Proficient in designing and optimizing Jenkins pipelines for seamless code integration, testing, and deployment workflows. ~ Linux Systems Mastery: Strong background in Linux system administration, scripting, performance tuning, and server management. ~ Automation Enthusiast: Adept at automating repetitive tasks and complex workflows with Ansible, shell scripting, and YAML-driven configurations. ~ Problem Solver: Quick to learn emerging technologies, troubleshoot complex issues, and optimize systems for maximum reliability and efficiency. ~ Detail-Oriented: Committed to delivering clean, secure, and high-quality solutions that meet both business and technical goals. Core Competencies ~ DevOps Tools: Jenkins, Docker, Kubernetes, Ansible, Terraform, Git, Bitbucket, YAML ~ Infrastructure as Code: Terraform, Ansible ~ Containerization and Orchestration: Docker, Kubernetes ~ Continuous Integration/Continuous Deployment: Jenkins Pipelines, GitOps workflows ~ Operating Systems: Linux (Ubuntu, CentOS) ~ Scripting: Shell scripting, YAML ~ Monitoring & Logging: (Optional, if you know Prometheus, Grafana, or similar tools.) ~ Project Management: Jira, Slack, Agile Methodologies