Skip to main content

Command Palette

Search for a command to run...

Bash Scripting

Published
3 min readView as Markdown
Bash Scripting
S

🔹 Azure Hybrid Cloud Engineer | DevOps Designing, automating, and scaling secure infrastructure across on-prem and Azure and AWS cloud infra.

☁️🔧 Skilled in IaC (Terraform), CI/CD (GitHub Actions, Azure DevOps), scripting (PowerShell, Bash), and containerization (Docker, Kubernetes)

🐳⚙️ Passionate about bridging the gap between legacy systems and modern cloud-native solutions

💡🚀 On a mission to automate everything and simplify the complex.

Optimize Efficiency with Bash Scripting by Easily Automating Repetitive Tasks


💡 Bash scripting is an essential skill for Linux users, enabling automation, system configuration, and workflow optimization. Whether you're just getting started or aiming to refine your expertise, this guide covers everything from basic scripting principles to advanced techniques for writing efficient Bash scripts.

🔹 1. Getting Started with Bash Scripting

📝 Setting Up Your First Script

To create a Bash script:

1️⃣ Open a Linux terminal and create a new script file:

nano my_script.sh

2️⃣ Add a shebang (#!) to specify Bash as the interpreter:

#!/bin/bash
echo "Hello, Bash scripting!"

Save the file using CTRL+O, hit enter, then CTRL+X to exit.

3️⃣ Grant execution permissions:

chmod +x my_script.sh

4️⃣ Run the script:

./my_script.sh

🏗 Variables and User Input

Define variables and accept user input:

name="Shams"
echo "Hello, $name!"

read -p "Enter your name: " user_name
echo "Welcome, $user_name!"

🔄 Conditional Statements

Control execution flow with if-else statements:

#!/bin/bash
read -p "Enter a number: " num
if [ "$num" -gt 10 ]; then
    echo "The number is greater than 10."
else
    echo "The number is 10 or less."
fi

🔹 2. Intermediate Bash Scripting Techniques

🔁 Loops for Automation

For Loop - Iterating through a sequence:

for i in {1..5}; do
    echo "Iteration $i"
done

While Loop - Running a task until a condition is met:

count=1
while [ $count -le 5 ]; do
    echo "Count: $count"
    ((count++))
done

🎯 Functions in Bash

Encapsulate code into reusable functions:

greet() {
    echo "Hello, $1!"
}
greet "Shams"

📂 Working with Files

Check if a file exists before processing it:

file="example.txt"
if [ -f "$file" ]; then
    echo "File exists."
else
    echo "File does not exist."
fi

🔹 3. Advanced Bash Scripting Techniques

📌 Array Handling

Store multiple values in an array:

fruits=("Apple" "Banana" "Cherry")
echo "First fruit: ${fruits[0]}"

🛠 Regular Expressions in Bash

Extract patterns using grep:

echo "User ID: 12345" | grep -o '[0-9]\+'

Error Handling & Debugging

Improve script reliability with set:

set -e  # Exit on error
set -u  # Treat unset variables as errors
set -x  # Debug mode to show command execution

Parallel Processing with xargs

Optimize batch processing:

echo -e "file1.txt\nfile2.txt\nfile3.txt" | xargs -n 1 cp /backup/

🎭 Handling Background Processes

Run long tasks in the background:

long_running_task &
echo "Task started in background!"

Conclusion

Bash scripting is a powerful tool for automating repetitive tasks, managing Linux systems efficiently, and enhancing productivity. By mastering basic commands, loops, error handling, and advanced techniques, you unlock new capabilities in system administration and development.

🚀 Have thoughts or suggestions? Drop them in the comments! Happy scripting!


🔗 Share your thoughts! If this article helped you, feel free to repost or share your Bash scripting insights on Hashnode! 💬