Monday, March 14, 2011

Hello World shell script dissected

1. Specific shell invocation
#!/bin/bash - It is called hash-bang or shebang statement. Presence of this statement in the beginning of any shell script is to invoke the specific shell to execute the script

/bin/bash  : Absolute path of the bash shell

Note 1: Shebang statement should be precisely in the first line. If it is given in other than first line, it will be considered as comment
Note 2: Shebang statement is not needed, if your script will execute in any shell

2. Comment for easy understanding of script
#Script to demonstrate - It is called comment. Anything comes after hash(#) will be considered as comment by shell and hence it will not be executed.

Note 1: #! will be executed by shell
Note 2: Shell supports single line comment only.`If you want to comment multiple line means you need to comment each line with hash(#) at the beginning of line.


3. Unix command or Shell programming statements
echo "Hello World!" - It is just a print command.  By default echo command will print its arguments to standard output

4. Script naming convention
hello.sh - It is just a script file name. sh extension is added to the script file purely for our convenience. No matter whatever extension we will give, UNIX/Linux just ignores it.

5. Making the script executable

chmod u+x hello.sh  - By using chmod command, we are giving execute permission to the owner of the file. Because without execute permission, we cannot execute the script directly.

6. Starting the script execution
./hello.sh  - This is invoking the script for execution.

Note: Different type of execution will be explained in the next post.

No comments:

Post a Comment