Creating and Executing first bash script

In this article we are going to cover the following topics:

Open your favourite text editor like leafpad,gedit or bluefish. you can use terminal based editors like vim and nano .

Insert the following lines of code into your editor and save the file.

#This is the first bash script
echo "Hello world good day/night"

The first line of the code is known as shebang.This is absolute path to the bash interpreter. Bash interpreter is installed inside #!/bin/bash (bin) directory. You can check all the executable on Linux type ls /bin.

Second line which is starting with # is called comment.This line is completely ignored by interpreter. Anything which starts with # is ignored.If you see shebang there is an exclamation sign added after the # .So interpreter can recognize difference between path and comment. Adding comments is good practice,with comments you can tell others what your code does.

Third line prints text on output screen.echo is an inbuilt function in bash shell that is used to display strings or text on output screen.

Setting executable permission on bash script

Open your terminal and type the following command.
I saved file with bashscipt1 name so replace this with yours.

root@seven:~# chmod +x bashscipt1

The above command will give execute permission to the owner and group. Now this file is ready to run.

root@seven:~# ./bashscipt1

That's it you have created and executed first bash script as simple as that.