[Techtalk] Comments in bash scripts

Travis Casey efindel at earthlink.net
Mon Feb 16 08:23:48 EST 2004


On Sunday 15 February 2004 18:06, showercurtain wrote:

> Someone told me recently that you mustn't put comments before the
> hash bang line in a bash script because it's bad practice.  Why is
> that?

The OS only checks the first line of a script for the #! .  Thus, if you put 
comments before there, your script will get run with whatever the user's 
current shell is, rather than the shell it should be run with.  Try this:

---- test.csh ----
# this is a test
#!/bin/csh
setenv test yes
echo $test
------------------

Run this script under bash, sh, ksh, or some other Bourne-type shell, and 
you'll get an error.  "setenv" isn't a command in those; the syntax to do 
the same thing would be:

test=yes
export test

Since the #! is on the second line, it doesn't get "seen" by the OS, so it 
gets executed with whatever the user's current shell is.

--
       |\      _,,,---,,_     Travis S. Casey  <efindel at earthlink.net>
 ZZzz  /,`.-'`'    -.  ;-;;,_   No one agrees with me.  Not even me.
      |,4-  ) )-,_..;\ (  `'-' 
     '---''(_/--'  `-'\_) 



More information about the Techtalk mailing list