In the release for Bash-5.0 it is mentioned that they added

BASH_ARGV0: a new variable that expands to $0 and sets $0 on assignment.

What is the purpose of this? When is this an improvement over just using $0?


The changelog says

    - BASH_ARGV0: new dynamic variable, returns $0 on reference and sets
      $0 on assignment.  From a suggestion from  Rocky Bernstein <rocky@gnu.org>
      a few years ago

Rocky Bernstein is, among other things, the initial author of the BASH Debugger. When debugging, it can be useful to be able to set $0 to different values; this led to this suggestion, ten years ago, of a set0 built-in which would allow just that. I think BASH_ARGV0 is the equivalent in the form of a special variable rather than a built-in.

$0 only allows you to read the value; before Bash 5, it could only be set when starting the shell. BASH_ARGV0 allows you to write the value as well as read it. 0 isn’t a valid variable name for assignment purposes, and enabling 0=... would have been rather more complex.

  • 3
    One might ask why the need for a new variable, instead of just assigning to $0 directly. But since 0 is not a valid variable name, 0=value is not recognized as an assignment, and changing that would be more involved (and backward-incompatible) – ilkkachu Jan 8 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.