Format of a bash script file

Share this post to your friends !

This entry is part 4 of 6 in the series Writing your first script
Format of a bash script file
5 (100%) 1 vote[s]

WriteBash - 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.


# This is your first script - Hello World script.

echo "Hello World"
format-of-a-bash-script-file 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:


# 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.

# Your first comment
# Your second comment
# ...
# Your n comment

# Define your fuctions or put your commands here

# Call your functions if it's exist

# Exit from script

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.

Continue reading the series«« Previous part: How to write a bash scriptNext part: Assign executation permission for a bash script file »»
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.

Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.