How to Write and Run First Bash Script

How to Write and Run First Bash Script
How to Write and Run First Bash Script

If you’re new to the world of Linux Administration and scripting, writing your first Bash script might seem intimidating. However, fear not! Bash scripting is a very beginner-friendly way to automate tasks and interact with the command-line interface, it is the first step to automation. In this article, we’ll guide you through the process of writing and running your very first Bash script.

Read More : Introduction to Bash scripting

Getting Started

To begin, open a text editor on your system. You can use your favorite plain text editor like vi or nano.

If you do not know VI editor read: How to use vi editor

Write the First Bash Script

In the text editor, type the following lines to create your Hello World Bash script:

If you are using vi editor give the name with .sh extension. Like below example

  • # vi hello_world.sh

#!/bin/bash
# This is a simple Hello World Bash script
echo "Hello, World!"

Let’s understand the code :

#!/bin/bash: The first line is called the “shebang,” which tells the system to use the Bash interpreter to run the script. It ensures that the bash script is executed using the correct shell. Sometimes it can be changed if you are using a customized system otherwise it will be the same for all Linux systems.

# This is a simple Hello World Bash script: This line is a comment. Comments start with the # symbol and are ignored by the Bash interpreter. They provide useful information to user or if you share the script with some other guy this will tell about the script’s purpose and help understand the code.

echo "Hello, World!": The command echo in Linux Bash is used to display messages on the terminal or shell. Our script will print “Hello, World!” when the script is executed. In this place, you can use any command like ls, df -h, or any it will run.

Save the Script

After writing the script, save the bash script with a meaningful name and the .sh extension. For example, you can save it as hello_world.sh in our case. If you are using vi or nano then we may have already given it a meaningful name and saved it with press the esc button and writing “:wq!” press enter key.

Make the Script Executable

Before running our bash script, we need to make it executable. Open your terminal or command prompt and navigate to the directory where you saved the script or you have to provide the path as well with the script. Then, use the following chmod command to give execute permissions to the script:

# chmod +x hello_world.sh

The chmod command changes file permissions, you can use the man command to check more usages and +x makes the script executable for the current user.

Run the Script

From the above action, your script is now executable, it’s time to run it! In the terminal or command prompt shell, enter the below command:

./hello_world.sh

or

sh hello_world.sh

The ./ before the script name specifies the current directory and default shell, and then the script name is provided. sh is also to tell the script is a shell script and runs from the default shell. Press Enter to run script , and you should see the output:

Hello, World!

Congratulations! You have successfully written and run your first Hello World Bash script journey. Now you are one step ahead.

Troubleshooting

If you encounter any issues running the bash script, follow the below steps to troubleshooting :

  • The shebang (#!/bin/bash) is the first line of the bash script.
  • The bash script file should have the .sh extension.
  • The script has to execute permissions (use chmod +x) or use chmod u+x script name.
  • You are running the script from the correct directory (use ./ before the script name). If not give a full path like ./home/username/hello_world.sh.

Conclusion

Writing and running your first Hello World Bash script is an exciting milestone in your journey as a Linux automation programmer. With this basic foundation, you can continue exploring Bash scripting and its many possibilities to automate tasks and reduce manual errors. As you gain more experience with time, you’ll find yourself creating powerful scripts to automate tasks and streamline your workflow. So keep practicing, experiment with different commands in place of echo, and embrace the world of Bash scripting! Happy scripting!

Leave a Comment

Your email address will not be published. Required fields are marked *