Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #15452

Re: ARGV[@] Not Always Populated

From Chet Ramey <chet.ramey@case.edu>
Newsgroups gnu.bash.bug
Subject Re: ARGV[@] Not Always Populated
Date 2019-10-02 09:10 -0400
Message-ID <mailman.754.1570021822.2651.bug-bash@gnu.org> (permalink)
References <CAOQZJ0GjaAvOO6VMppz=QHszASUxs0jfTFi+OHmLPEK50e+U1Q@mail.gmail.com> <ec3ec610-ac8e-4ae2-7620-8628cd117351@case.edu>

Show all headers | View raw


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/

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: ARGV[@] Not Always Populated Chet Ramey <chet.ramey@case.edu> - 2019-10-02 09:10 -0400

csiph-web