[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: last `set option value' always passed as $1 and $2 during a 'source'

From: Philip Lijnzaad
Subject: Re: last `set option value' always passed as $1 and $2 during a 'source' ??
Date: 12 Jun 2001 09:43:59 +0100

On 11 Jun 2001 14:25:03 -0400, 
"Paul" == Paul Jarc <address@hidden> wrote:

Paul> Philip Lijnzaad <address@hidden> writes:
Paul> If a sourced script expect parameters, the caller might choose to
Paul> provide them by using 'set' rather than by passing them on the
Paul> 'source' command line.  
>> But if I want to write a general purpose little script, it can't use any
>> arguments, because it doesn't know if these arguments were explicitly typed
>> in, or where still 'lying around' in the environment (which was the bug I was
>> hunting). 

Paul> It can use arguments.  It can't tell how the caller specified the
Paul> arguments, but it shouldn't care.  If it got the wrong arguments
Paul> because the caller didn't pass any extra arguments on the 'source'
Paul> command line and had some extra arguments left in its own list, then
Paul> that's a bug in the caller, not the script.  

I have to admit defeat here, because all the /bin/sh's I could lay may hands
on (sgi, sun, osf, linux) have the behaviour of assigning set'ed variables to
$1, $2 etc. So bash will have to emulate this.

Paul> The bug should be fixed,
Paul> not worked around, and it's not necessary to avoid useful
Paul> functionality such as passing arguments to sourced scripts.  The
Paul> caller simply must take care to correctly use the interface of
Paul> 'source' and the script.

Paul> I agree that the interface of 'source' is not a good one, and it can
Paul> lead to problems that may be hard to diagnose.  

Definitely. The problem, BTW, does not exist as such in plain /bin/sh, since
that won't pass arguments to a source'd (or should I say .-ed;-) script. I
guess the `set var[s]' -> $1, $2 etc. behaviour was intended exactly for the
use in source'd scripts.

Paul> But consistent awareness of the potential problems tends to prevent
Paul> them.

which is why Usenet is such a treasure trove :-)

>> It may be that this is Bourne-shell legacy,

Paul> It certainly isn't unique to bash.  Solaris sh doesn't even let you
Paul> pass arguments to the sourced script on the 'source' command line -
Paul> the sourced script always sees the caller's positional parameters.

I thought this actually was the standard behaviour; ash and osf1 sh also have
it like this. But after looking it turns out that sgi sh and ksh have it like
bash, so I don't know. 

If you have a procedure with 10 parameters, you probably missed some. (Kraulis)
Philip Lijnzaad, address@hidden \ European Bioinformatics Institute,rm A2-08
+44 (0)1223 49 4639                 / Wellcome Trust Genome Campus, Hinxton
+44 (0)1223 49 4468 (fax)           \ Cambridgeshire CB10 1SD,  GREAT BRITAIN

reply via email to

[Prev in Thread] Current Thread [Next in Thread]