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.
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.
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.
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.
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.
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.
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.
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.
Introduction Variables are one of the most important basics of Bash scripting. A variable is…
Introduction Running a Bash script in Linux is a basic but important skill for anyone…
Docker has become one of the most important tools in modern software development. If you…
The APT Command Linux users rely on is one of the most powerful tools for…
Ubuntu users usually install software through .deb packages or the APT package manager. However, some…
A reliable Pip Installation Guide is essential for anyone working with Python on Ubuntu. Pip…