How to write a bash script

Share this post to your friends !

This entry is part 3 of 6 in the series Writing your first script
How to write a bash script
5 (100%) 2 vote[s]

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.

how-to-write-a-bash-script How to write a bash script
Use the Gedit editor to write a bash script.

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 writebash.com.

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 /home/danie/Desktop folder.

danie@linuxmint ~ $ cd /home/danie/Desktop/
danie@linuxmint ~/Desktop $ ./writebash.com

The important command when executing it is ./writebash.com with the filename writebash.com

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.

Continue reading the series«« Previous part: What is bash script?Next part: Format of 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.