Difference between #!/bin/sh and #:/bin/sh
Someone开发者_开发技巧 send me a script today starting with #: and after googling I didn't found any answer.
Even if the script works I wonder what does that mean.
Wow! That's brings backs lots of memories!
Back in the 1980s and early 1990s, there were two basic shells, Bourne shell (/bin/sh
) and C shell (/bin/csh
).
Bourne shell had very few user friendly things. There were no aliases or command substitutions. Therefore, most people liked using C Shell as their default shell.
However, Csh was a terrible scripting language. (See http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/). Therefore, you used C shell as your shell, but wrote your scripts in Bourne shell which had a much better syntax.
However, there was a tiny little problem: Since your default shell is C Shell, typing a shell script name at the command line, and the C Shell would pick it up and try to execute it.
To get around this, you put :
as the first line in your program. This was a Bourne shell comment, but was an invalid C Shell command. Thus the script would fail to run if you forgot to put sh
in front of it.
Later on, systems would know if the first line was :
, it should be a Bourne shell script. And, even later, you could put #:
so it would be a comment and not a command. Some people put the name of the shell, /bin/sh
next to it to help remind people were suppose to run this as a Bourne shell script.
C shell started dying out after Kornshell started becoming popular. It was about this time when the shebang (#!
) came out, but that was only for AT&T and not the Berkeley derived systems. BSD systems didn't get the shebang until the late 1980s. And, Sun people used C Shell as their default shell until Solaris came out.
I hadn't seen a program begin with #: /bin/sh
in ages.
BTW, it is common to start your scripts this way:
#! /usr/bin/env perl
This way, you use the version of Perl that's in your path and don't have to worry what directory it is in. For example, if you begin your script with:
#! /usr/local/bin/perl
And, Perl is actually in /usr/bin
, your script won't run. The env
program is always in /usr/bin
and is guaranteed to work. Of course, if you want to use a specific version of Perl and not the one that is in the path, you'd go with the first method.
This is a comment only...
#:/bin/sh
a shebang is #!. Everything else starting with a # is a comment.
example:
run a script starting with
#!/usr/bin/perl
and it'll be run by perl. Now, replace the '!' by ':' and you'll run it with the defaut interpreter, which is your shell.
精彩评论