The Beginners Guide to Shell Scripting

The Beginners Guide to Shell Scripting

In this tutorial we will discuss about the topic The Beginners Guide to Shell Scripting.

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.

Was this Tutorial helpful? Help others share on Facebook, Twitter, and Google Plus!

 
Enjoyed this video?
The Beginners Guide to Shell Scripting
"No Thanks. Please Close This Box!"