DEV Community

Cover image for Mastering Bash Scripting: A Step-by-Step Guide 🚀
FARUKH KHAN
FARUKH KHAN

Posted on

Mastering Bash Scripting: A Step-by-Step Guide 🚀

Bash scripting is a powerful tool for automating tasks and managing systems. Whether you're a sysadmin, developer, or DevOps engineer, understanding Bash scripts is crucial for your toolkit. In this post, we'll walk through several key tasks that highlight essential features of Bash. Let’s get scripting! 🖥️

Task 1: Comments 📝

Comments make scripts easier to understand. They begin with # and are ignored during execution.

Script Example:

#!/bin/bash
# This script explains how comments work in Bash
<<comment
This script demonstrates the use of comments in Bash
Author: Farukh Khan
comment

echo "This is a script with comments."
Enter fullscreen mode Exit fullscreen mode

Execution:

chmod +x script.sh
./script.sh
Enter fullscreen mode Exit fullscreen mode

Output:

This is a script with comments.

Enter fullscreen mode Exit fullscreen mode

Task 2: Echo Command 📢

The echo command is used to print messages to the terminal.

Script Example:

#!/bin/bash
echo "Hello, DevOps world! Ready to automate all the things?"
Enter fullscreen mode Exit fullscreen mode

Execution:

chmod +x script.sh
./script.sh
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, DevOps world! Ready to automate all the things?

Enter fullscreen mode Exit fullscreen mode

Task 3: Variables 📊

Variables store data in Bash. They can be used by simply referencing them with a $.

Script Example:

#!/bin/bash
greeting="Hello"
name="Farukh"
echo "$greeting, $name!"
Enter fullscreen mode Exit fullscreen mode

Execution:

chmod +x script.sh
./script.sh
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, Farukh!

Enter fullscreen mode Exit fullscreen mode

Task 4: Arithmetic with Variables ➕🧮

You can perform arithmetic with variables using $((...)).

Script Example:

#!/bin/bash

# Taking input
echo "Enter first number:"
read num1
echo "Enter second number:"
read num2

# Calculating sum
sum=$((num1 + num2))

echo "The sum of $num1 and $num2 is $sum"
Enter fullscreen mode Exit fullscreen mode

Execution:

chmod +x script.sh
./script.sh
Enter fullscreen mode Exit fullscreen mode

Output:

Enter first number:
5
Enter second number:
10
The sum of 5 and 10 is 15
Enter fullscreen mode Exit fullscreen mode

Task 5: Built-in Variables 🔍

Bash provides several built-in variables like $USER, $HOME, and $PWD.

Script Example:

#!/bin/bash
echo "User: $USER"
echo "Home Directory: $HOME"
echo "Current Directory: $PWD"
Enter fullscreen mode Exit fullscreen mode

Execution:

chmod +x script.sh
./script.sh
Enter fullscreen mode Exit fullscreen mode

Output:

User: ubuntu
Home Directory: /home/ubuntu
Current Directory: /home/ubuntu/scripts
Enter fullscreen mode Exit fullscreen mode

Task 6: Wildcards for Pattern Matching 📂

Wildcards such as * help match file patterns. Here, we list .txt files.

Script Example:

#!/bin/bash

# List all .log files in the current directory
echo "Log files in this directory:"
ls *.log

# List all .sh files in the parent directory
echo "Bash scripts in the parent directory:"
ls ../*.sh
Enter fullscreen mode Exit fullscreen mode

Execution:

chmod +x script.sh
./script.sh
Enter fullscreen mode Exit fullscreen mode

Output:

Log files in this directory:
access.log
error.log
Bash scripts in the parent directory:
script.sh
backup.sh
Enter fullscreen mode Exit fullscreen mode

Wrapping Up 🎁

There you have it, folks! We've covered comments, echo, variables, arithmetic operations, built-in variables, and wildcards. These are the building blocks of Bash scripting that'll take your DevOps game to the next level.

Remember, the key to mastering Bash (or any scripting language) is practice. So fire up that terminal and start coding! 💻

Happy scripting, and may the DevOps force be with you! 🚀✨

DevOps #BashScripting #Automation #Linux #ShellScripting #DevOpsTools #ContinuousIntegration #ContinuousDeployment

Top comments (0)