πŸš€ Bash Scripting Made Easy – Learn by Example (With Outputs!)

VinuthnaVinuthna
4 min read

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 variable

  • Quotes 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 makes read 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 when if 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 conditions

  • else 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 comparison

  • Use != 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 exists

  • You 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

0
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