The first step in learning shell scripting

In this article I will explain you what the sha-bang (also known as shebang) is and will create and exec the Hello World script.

What is the sha-bang (shebang)?

The shebang is the “#!” at the head of your script. It tells the operating system that the file is a set of commands that can be interpreted with a certain program. Following the shebang is the path of the program that interprets the script.

Example of shebang lines:

#!/bin/bash
#!/bin/sh
#!/bin/csh
#!/usr/bin/perl
#!/bin/sed -f
#!/bin/awk -f

If the shebang line is missing from your script, it will be interpreted by the default system shell (usually bash on Linux and csh on *BSD).

How to create your first script:

I will use vim to write this in the myscript.sh file:

$ vim myscript.sh
#!/bin/bash
echo "Hello World!"
[:wq]

How to invoke the recently created script with source or bash:

Now that the script was created, let’s exec it:

$ source myscript.sh
OR: $ bash myscript.sh
Hello World!

You can also set rx permissions to your script and exec it as an executable file:

$ chmod +rx myscript.sh
OR:
$ chmod 555 myscript.sh

And exec it:

$ ./myscript.sh
Hello World!

If you liked this , read the next shell scripting article.

Liked it? Take a second to support Geekster on Patreon!
Tagged with: , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Support LinuxG via Patreon
Support LinuxG.net on Patreon!
Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!