WriteBash - How to write a bash script? What steps does it include? And how will we do it? This article will show you an overview of how to write a bash script.
To create and run a shell/bash script, we will do the following 3 steps.
Write an bash script
Essentially, bash scripts are just a text file. So, to write a bash script file, we need an editor to write it.
On Linux there are many different editors that you can freely use. However, to write scripts smoothly, you should choose an editor that supports syntax highlighting.
The editors support syntax highlighting that allows displaying the color of the lines of code. This will make it easier to see, easily identify the wrong or simply look better.
What is the content of this script file? These are the commands that you type in the Terminal window. Instead of typing each command one by one, you put all the commands into this script file and execute it only once.
Make the script executable
Because as you read above, bash script is actually a text file. On Linux systems there are certain restrictions on permissions. A normal text file will not be considered a program and it cannot be executed.
So, in order to execute the bash script file that you just wrote, you need to assign the executable permissions to it.
At this point, you don’t need to understand what this command really is. You just need to know that you can use it to assign execution permissions to scripts.
danie@linuxmint ~/Desktop $ chmod a+x writebash.com
The above command will grant execution permissions for the script file named
Put the script somewhere and execute it
Now, put the script in any directory you want on the Linux system. You can move into that directory and execute the script with the following command, for example I am putting the script in the
danie@linuxmint ~ $ cd /home/danie/Desktop/ danie@linuxmint ~/Desktop $ ./writebash.com
The important command when executing it is
./writebash.com with the filename
Because the bash script file starts by calling the
#! /bin/bash library, you may not need to move into the directory containing the script and still be able to execute it. With the following command.
danie@linuxmint ~ $ /bin/bash /home/danie/Desktop/writebash.com
The understandable syntax of the above command is (without square brackets):
[/bin/bash] [absolute path to file]
So basically you’ve got 3 steps to write a bash script. In the following articles, we will learn more about 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.