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

xargs Command in Linux: Build and Execute Commands from Input

The xargs command in Linux reads items from standard input and passes them as arguments to another…

1 day ago

locate Command in Linux: Find Files Fast with a Database Search

The locate command in Linux searches for files and directories by name. It queries a pre-built database…

1 day ago

Listing Linux Services with systemctl: A Complete Guide

Most modern Linux distributions use systemd as the default service manager. Knowing how to list…

1 day ago

How to Truncate Files in Linux: Empty Files Without Deleting

Truncating a file in Linux means removing its contents while leaving the file itself in…

1 day ago

ss Command in Linux: Display Socket Statistics and Connections

The ss command in Linux lists open sockets and active network connections. It replaced the deprecated netstat command and…

2 days ago

ftp Command in Linux: Connect to Servers and Transfer Files

The ftp command in Linux connects to a remote FTP server and transfers files. FTP transmits everything…

2 days ago