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)