Header AD

How To Create And Execute A Bash Script File

How To Create And Execute A Bash Script File

How To Create And Execute A Bash Script File
How To Create And Execute A Bash Script File

Creating and Executing first bash script

In this article we are going to cover the following topics:
  • Creating first bash script.
  • Setting up an executable permission on the script.
  • Making Comments.
  • Basic Syntax and Shebang.
Open your favorite text editors like LeapPad, Gedit or bluefish. you can use terminal based editors like Vim Editor and nano.

Insert the following lines of code into your editor and save the file.
#!/bin/bash 
#This is the first bash script
echo "Hello world good day/night"
 
#!/bin/bash
The first line of the code is known as shebang.This is the 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.

The second line which is starting with # is called comment.This line is completely ignored by the interpreter. Anything which starts with # is ignored.If you see shebang there is an exclamation sign added after the #. So interpreter can recognize the 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 the 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.


So above are the How To Create And Execute A Bash Script File. Hope you like this article, keep on sharing with others too. Also, share your experience with us in a comment box below.

No comments