Is this a bug in sh?

terryl at tekcrl.LABS.TEK.COM terryl at tekcrl.LABS.TEK.COM
Thu Jul 6 04:18:27 AEST 1989


In article <PAUL.89Jul4155856 at marvin.moncam.co.uk> paul at moncam.co.uk (Paul Hudson) writes:
>
>Am I just showing my ignorance of sh?
>Consider the two (executable) shell scripts..
>
>script1 is		and  	script2 is
>
>echo $0				echo $0
>name=`basename $0`		name= `basename $0`
>				     ^
>			Note the space here.
>
>executing script1 prints "script1" as you would expect.  Executing
>script2 prints "script2" "script2" ...... and spawns shells at an
>enormous rate.
>
>This is sunos4.0 on a sun3/50.
>
>Go on, tell me I haven't RTFM .... ;-) Then tell me why it's this way.


     HOK, I'll tell ya "You haven't RTFM", but unfortunately, for this example
you have to read between the lines in TFM. What is happening in script2 is you
are assigning a null string to the variable name (by virtue of what you noted
"Note the space here."); in sh it is possible to have a command line of the
form:

	<variable-name>=<value-to-bet-set> <real-command-line>

      What this does is to put whatever <variable-name> is into the environment
variables, but only for the execution of <real-command-line>. So in your example
above, in script2 you are assigning the null string to the variable name, and
then putting the variable name into the environment temporarily for the running
of the command `basename $0` (which is script2, in your case). That's why
script2 keeps printing out "script2" "script2" and spawns shells at an enormous
rate.......


Help
Stamp
Out
Fascist
News
Software
!!!!
!!!!



More information about the Comp.unix.questions mailing list