2

When using bash, there are self-referential variables that don't seem to be available in zsh. For example, $? gets the exit status of the most recent foreground process, $_ gets the last parameter of the previous command, etc.

Are there equivalents to these in zsh? I ask with reference to this question for bash.

efthimio
  • 592
  • 5
  • 19
  • 2
    Here's a good place to start: [Variables Index](http://zsh.sourceforge.net/Doc/Release/Variables-Index.html), [Parameters Set By The Shell](http://zsh.sourceforge.net/Doc/Release/Parameters.html#Parameters-Set-By-The-Shell). I think `$argv[1]` is the equivalent of `$1` and `$_` is the same, but I'm not a zsh expert. – wjandrea Dec 20 '19 at 01:06
  • 1
    `$1`, `$2` exist in `zsh`. They're required by POSIX. – Barmar Dec 20 '19 at 01:12
  • @Bamar good point, I changed the example – efthimio Dec 20 '19 at 01:20
  • 1
    `$?` should work, too. `zsh` should implement all the standard POSIX variables. – Barmar Dec 20 '19 at 01:28
  • And so does `$_`, although it's not required by POSIX. – Barmar Dec 20 '19 at 01:29
  • You can find all the POSIX special variables at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05_02 – Barmar Dec 20 '19 at 01:32
  • Here is the command I'm trying to run: `command -v python3.7 &> /dev/null; if [[ $? -eq 0 ]]; then echo "Python version $_ is currently installed."; else echo "Python version $_ is NOT installed."; fi`. The final print statement runs but the `$_` variable displays nothing (whereas in `bash` it does display correctly). – efthimio Dec 20 '19 at 01:33

1 Answers1

3

bash calls these the special parameters (to distinguish them from ordinary variables and the positional parameters). zsh implements essentially the same set, but documents them along with other parameters set by the shell, though it does also tag each one as special.

See man zshparam:

PARAMETERS SET BY THE SHELL
       In the parameter lists that follow, the mark `<S>' indicates  that  the
       parameter  is  special.   `<Z>'  indicates  that the parameter does not
       exist when the shell initializes in sh or ksh emulation mode.

       The following parameters are automatically set by the shell:

       ! <S>  The process ID of the last command  started  in  the  background
              with &, or put into the background with the bg builtin.

       # <S>  The  number of positional parameters in decimal.  Note that some
              confusion may occur with the syntax  $#param  which  substitutes
              the  length of param.  Use ${#} to resolve ambiguities.  In par-
              ticular, the sequence `$#-...' in an  arithmetic  expression  is
              interpreted as the length of the parameter -, q.v.

       ARGC <S> <Z>
              Same as #.

       $ <S>  The  process  ID  of  this  shell.  Note that this indicates the
              original shell started by invoking  zsh;  all  processes  forked
              from  the  shells  without executing a new program, such as sub-
              shells started by (...), substitute the same value.

       [... etc ...]
chepner
  • 497,756
  • 71
  • 530
  • 681