You are currently viewing Bash Scripting Step by Step

Bash Scripting Step by Step

  • Post author:
  • Post category:Bash
  • Post comments:0 Comments
  • Post last modified:February 23, 2024

Bash (Bourne Again Shell) is a command language interpreter for Unix and Unix-like operating systems. It is widely used for automating tasks, writing system administration scripts, and more. In this tutorial, we’ll cover the basics of Bash scripting step by step.


Before getting started, ensure you have:

  • Access to a Unix or Unix-like operating system (Linux, macOS, etc.).
  • A text editor like Vim, Nano, or Visual Studio Code.
  • Basic knowledge of Unix/Linux command-line operations.

Step 1: Creating Your First Script

  1. Open your preferred text editor.
  2. Create a new file with a .sh extension (e.g.,
  3. Add the following line at the beginning of the script to specify the interpreter: #!/bin/bash
  4. Write your script below this line.

Step 2: Hello World Script

Let’s start with a simple “Hello, World!” script.


echo "Hello, World!"

Step 3: Running Your Script

After saving your script, you need to make it executable.

chmod +x

Now, you can run your script:


You should see the output: Hello, World!

Step 4: Variables and User Input

Bash allows you to use variables to store data. You can also read input from users.


# Define a variable

# Read input from user
echo "Enter your age:"
read age

# Print a message using variables
echo "Hello, $name! You are $age years old."

Step 5: Conditional Statements

Bash supports conditional statements like if, elif, and else.


echo "Enter a number:"
read num

if [ $num -gt 0 ]; then
    echo "The number is positive."
elif [ $num -lt 0 ]; then
    echo "The number is negative."
    echo "The number is zero."

Step 6: Loops

You can use loops in Bash like for and while.


# For loop
echo "Counting to 5:"
for i in {1..5}; do
    echo $i

# While loop
echo "Counting down from 5:"
while [ $num -gt 0 ]; do
    echo $num


With these fundamentals, you can start automating tasks, writing more complex scripts, and exploring advanced Bash features. Keep practicing and experimenting to become proficient in Bash scripting.

Leave a Reply