Format of a bash script file

by Daniel Pham
Published: Updated:
This entry is part 4 of 6 in the series Writing your first script

After you’ve read the steps to write a bash script file, what’s in that bash script file? What is the format of a bash script file?

“Hello World” script

To keep things simple and familiar, I’ll start with a script called “Hello World”. This is probably a type of file that you will probably encounter in any programming language.

Now open the editor on your computer (probably Gedit) and start typing the text below into the file.

#!/bin/bash

# This is your first script - Hello World script.

echo "Hello World"
format of a bash script file
A bash script name “Hello World”.

On it, is the entire content of a “Hello World” bash script file. Really simple right, only 3 lines of text.

Understand format of a bash script file

Now we will find out what each line means. I will go from the bottom up on the file.

The third line, the echo command, you will find it very familiar because it appears in many different programming languages. It is simply a command to echo a text inside double quotation marks (or within single quotes).

The second line, this is the line that shows the author’s comment content. Like other programming languages, lines beginning with # are interpreted as comments in files and will be ignored by the system when executing scripts.

In fact, you can comment right above or behind a command. Like this:

Above:

# This is your first script - Hello World script.

echo "Hello World"

Or behind on the same line:

echo "Hello World" # This is your first script - Hello World script.

And the first line, you see the # at the beginning of the line, but don’t misunderstand, this is not a comment.  The line #! in the script file it is called a shebang.

The shebang is used to tell the system the name of the interpreter that should be used to execute the script that follows. A bash script should include #!/bin/bash as its first line.

You can understand that this shebang tells the operating system that this is a script file, and this script file uses the bash library to execute commands in the file.

Summary format of a bash script file

Okey, you understand the “Hello World” script, it’s really simple. So what if you want to write another script? Where do you put the commands?

The text below is a standard format of a bash script file. In the file I mentioned the function, temporarily don’t bother with what it is and how to write it. You only consider the function to have the same format as a normal command.

#!/bin/bash
# Your first comment
# Your second comment
# ...
# Your n comment

# Define your fuctions or put your commands here
functions/commands

# Call your functions if it's exist
functions-name

# Exit from script
exit

Through this article, you can already grasp what a bash script file will contain in it. Practice writing your script files with the simple commands you learned in the previous articles. Just write the script, save the script file, assign execution permissions to the script and execute it.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating
Series Navigation«« Previous part: How to write a bash scriptNext part: Assign executation permission for a bash script file »»

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.