Cybersecurity Updates & Tools

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.