Bash Scripting

How To Write Your First Bash Script In Linux Step By Step

Introduction

Writing your first Bash script in Linux is one of the best ways to start learning Linux automation. A Bash script is a simple text file that contains Linux commands. Instead of typing commands one by one in the terminal, you can save them inside a script and run them together.

Bash scripting is useful for beginners, system administrators, developers, and cybersecurity learners. It helps you automate daily tasks such as creating folders, checking system details, backing up files, reading logs, and running security commands. In this tutorial, you will learn how to create, save, give permission, and run your first Bash script in Linux.

What Is Needed To Create A Bash Script?

To write a Bash script, you only need a Linux terminal and a text editor. You can use editors like nano, vim, or gedit. For beginners, nano is the easiest option.

Open your terminal and create a new file:

nano first-script.sh

The .sh extension is commonly used for Bash scripts. It is not mandatory, but it helps you identify script files easily.

Add The Bash Shebang

Every Bash script usually starts with a shebang line. The shebang tells Linux which interpreter should run the script.

Add this line at the top of your file:

#!/bin/bash

Now add a simple command below it:

#!/bin/bash

echo "Hello, this is my first Bash script!"

The echo command is used to print text in the terminal.

Save The Bash Script

If you are using nano, save the file by pressing:

CTRL + O

Then press:

Enter

Exit the editor using:

CTRL + X

Now your first Bash script is created.

Give Execute Permission

Before running the script, you need to give it execute permission. Use the chmod command:

chmod +x first-script.sh

This command makes the script executable.

Run Your First Bash Script

Now run the script using:

./first-script.sh

You should see this output:

Hello, this is my first Bash script!

Congratulations! You have successfully created and executed your first Bash script in Linux.

Another Simple Bash Script Example

Let us create a script that shows basic system information:

#!/bin/bash

echo "System Information"
echo "Current User: $(whoami)"
echo "Current Directory: $(pwd)"
echo "Today Date: $(date)"
echo "Linux Kernel: $(uname -r)"

Save it as:

system-info.sh

Give permission and run it:

chmod +x system-info.sh
./system-info.sh

This script displays the current user, current directory, date, and Linux kernel version.

Conclusion

Writing your first Bash script in Linux is simple and powerful. You create a file, add the shebang, write commands, give execute permission, and run the script. Once you understand this basic process, you can start creating scripts for automation, system monitoring, file management, and cybersecurity tasks.

Bash scripting is an important skill for anyone learning Linux. Start with small scripts, practice regularly, and slowly build more useful automation scripts for real-world Linux tasks.

0xSnow

0xSnow is a cybersecurity researcher with a focus on both offensive and defensive security. Working with ethical hacking, threat detection, Linux tools, and adversary simulation, 0xSnow explores vulnerabilities, attack chains, and mitigation strategies. Passionate about OSINT, malware analysis, and red/blue team tactics, 0xSnow shares detailed research, technical walkthroughs, and security tool insights to support the infosec community.

Recent Posts

How To Use Variables In Bash Scripts

Introduction Variables are one of the most important basics of Bash scripting. A variable is…

42 minutes ago

How To Run A Bash Script In Linux Step By Step

Introduction Running a Bash script in Linux is a basic but important skill for anyone…

50 minutes ago

Install Docker on Ubuntu 24.04 With Easy Setup Guide

Docker has become one of the most important tools in modern software development. If you…

2 hours ago

APT Command Linux: Essential Package Management Guide

The APT Command Linux users rely on is one of the most powerful tools for…

5 hours ago

Install RPM on Ubuntu: Easy Guide to Run RPM Packages

Ubuntu users usually install software through .deb packages or the APT package manager. However, some…

8 hours ago

Pip Installation Guide for Ubuntu Python Setup

A reliable Pip Installation Guide is essential for anyone working with Python on Ubuntu. Pip…

23 hours ago