Example one
#!/bin/sh
# purpose: print out current directory name and contents
pwd
ls
Example two
# purpose: print out current directory name and contents
#!/bin/sh
pwd
ls
What is the difference – if I make the first line a comment(#
), with #!/bin/sh
as the second line, what will happen?
What is meaning of #!/bin/sh
?
Normally a shell script is run by your default shell defined in the /etc/passwd file. But you can define explicitly a program which can run your script.
Unices uses a common method to determine what program needed to run a specific script (man execve(2)). If the script has the proper execute rights set and in a script the first line starts with a #!
characters, it will run by the program defined afterwards.
For example if the first line is #!/usr/bin/awk -f
then the rest of the file will be passed to the awk
program (so it has to use awk
syntax). Or if a Makefile starts with #!/usr/bin/make -f
then the rest of the file will be passed to make
. You can start the script as a normal program and the script can be written in awk
or make
(or whatever defined) syntax.
If execve
does not find #!
as the first two character of the file, it will consider as a normal script file and it will run as it is.
So using #!
You can determine the script language and You do not need to know what shell is used by the other user using your script. In any other line #!
will be interpretered your default shell, which is usually just a comment line.