bug-bash
[Top][All Lists]
Advanced

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

Re: ARGV[@] Not Always Populated


From: Chet Ramey
Subject: Re: ARGV[@] Not Always Populated
Date: Wed, 2 Oct 2019 09:10:12 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.9.0

On 10/1/19 6:33 PM, Adam Danischewski wrote:

> If you don't access the ARGV array before you drop into a function, ARGV is
> not populated.
>       [...]
> If you uncomment the echo at the top of the script then the printing is as
> expected.
> I realize the Bash Documentation states:
>        BASH_ARGV
>               An  array variable containing all of the parameters in the
> current bash
>               execution call stack.  The final parameter of the last
> subroutine  call
>               is  at the top of the stack; the first parameter of the
> initial call is
>               at the bottom.  When a subroutine is executed, the parameters
>  supplied
>               are  pushed  onto BASH_ARGV.  The shell sets BASH_ARGV only
> when in ex‐
>               tended debugging mode (see the description of the  extdebug
>  option  to
>               the shopt builtin below).  Setting extdebug after the shell
> has started
>               to execute a script, or
> *referencing this variable when extdebug is  not              set, may
> result in inconsistent values.*

By default, the array is lazily populated: if you reference BASH_ARGV, and
it hasn't been set up, it will be initialized using the current positional
parameters.

> Many people are using BASH_ARGV:
> e.g.
> https://stackoverflow.com/questions/2740906/how-to-access-command-line-arguments-of-the-caller-inside-a-function
> If it's not too much trouble I think a lot of people would be glad if you
> could make this work in a standardized way.

Doesn't that suggest that the best way to use it in a `standardized' way is
to run `shopt -s extdebug' at the script's top level, before trying to
reference BASH_ARGV?

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

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