[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Inconsistency of the behavior of `source` with and witho
From: |
Peng Yu |
Subject: |
Re: [Help-bash] Inconsistency of the behavior of `source` with and without arguments in function |
Date: |
Wed, 22 Jul 2015 10:16:18 -0500 |
On Wed, Jul 22, 2015 at 8:51 AM, Chet Ramey <address@hidden> wrote:
> On 7/17/15 8:51 PM, Peng Yu wrote:
>> Hi, The following code shows that $@ is treated differently when
>> `source` is used with and without arguments in `function`. Isn't it
>> better to make `source` always pass "$@" from the source script back
>> to the external script whether arguments are used with `source` or
>> not?
>
> This was a conscious decision. `source' will temporarily set the
> positional parameters from arguments following the filename, so sourced
> files can be a little closer to scripts or shell functions. However,
> since `source' is supposed to act as if the commands it executes were
> entered directly, changes to the positional parameters made within the
> sourced file persist.
I agree changes in the positional parameters in the sourced file
should persist. But that should also be applied to the case when a
script is sourced with parameters as in `source source.sh "$@"`. After
all `source` is not function.
Alternatively, wouldn't it better to allow users to decide whether any
changes in "$@" should be persistent outside the sourced script.
> It has been this way for a very long time, back
> to the bash-1.x days.
>
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
> ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, ITS, CWRU address@hidden http://cnswww.cns.cwru.edu/~chet/
--
Regards,
Peng