In the bash v5.0 manual at page 74, there is the following documentation (and a similar one for BASH_ARGC).

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 extended debugging mode (see Section 4.3.2 [The Shopt Builtin], page 65, for a description of the extdebug option to the shopt builtin). 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.

I put in bold the sentences about which I would like to know more and in particular,

  1. Is there any way to be able to rely on these builtin variables beyond activating extended debugging mode? Asked differently, what does may in the last sentence above mean?

  2. If these builtin variables are thought for debug only (and the answer to 1. is no), is it correct to say that the best way to access the script command line arguments deep in some function call (i.e. in a function called by another function called by another function, etc.) is to store them before into a global array? Passing them around via "$@" would also in principle work, but it is not so handy sometimes.

I found this related answer that, indeed, says

You may have to turn on extended debugging in the shebang (e.g., #!/bin/bash -O extdebug) or with shopt (e.g., shopt -s extdebug), but it works for me in bash 4.2_p37 without it turned on.

  1. Bonus question. Why does it sometimes work without activating the extended debugging mode?
  • While breaking encapsulation may seem like a god like power. One must remember that encapsulation is one of the greatest gifts that programmers have been given, to allow them to write easy to read, bug-free, programs. Feb 20, 2020 at 19:59
  • @ctrl-alt-delor Your point of view is as interesting as cryptic and I would be interested in knowing to which kind of encapsulation you are thinking to - if we can really speak about encapsulation in bash. On the other hand, I opened here this question to learn more on this part/feature of bash to then use the language in an awarer way. Hence, thank you for adding your suggestion not to rely on this. Feb 21, 2020 at 8:59
  • see en.wikipedia.org/wiki/Encapsulation_(computer_programming) However in is not OOP. Functional and procedural have encapsulation i.e. a function or procedure (so long as global variables are avoided). Feb 21, 2020 at 9:26
  • Funnily enough, I read exactly that link before posting my comment. You have a point, sure, and also in C and C++ argc and argv are passed explicitly to the main function. Still, I would not say that something should not be understood, just because it should be better not to do it. Feb 21, 2020 at 14:58


You must log in to answer this question.

Browse other questions tagged .