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

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

1 week ago

git cherry-pick Command: Apply Commits from Another Branch

Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…

1 week ago

Best Email APIs for Secure Business Email: Why Developers Are Moving Beyond SMTP

Email is still one of the most important communication channels inside modern applications. Password resets,…

1 week ago

Nginx Commands in Linux: Start, Stop, Reload, Test, and Log

Nginx is a high-performance web server and reverse proxy trusted by some of the largest…

2 weeks ago

ufw Command in Linux: Manage Firewall Rules with Examples

ufw (Uncomplicated Firewall) sits on top of iptables (or nftables on newer systems) and replaces…

2 weeks ago

who Command in Linux: Show All Logged-In Users and Sessions

When you share a server with a team or investigate unexpected activity, the first question…

2 weeks ago