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.
#!/bin/bash # This is your first script - Hello World script. echo "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 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.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.
If you appreciate what we share in this blog, you can support us by:
- Stay connected to: Facebook | Twitter | Google Plus | YouTube
- Subscribe email to recieve new posts from us: Sign up now.
- Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
- Become a Supporter - Make a contribution via PayPal.
- Support us by purchasing Ribbon Lite Child theme being using on this website.
We are thankful for your support.