efthimio
efthimio

Reputation: 928

What are the special dollar sign variables in zsh?

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.

Upvotes: 2

Views: 6106

Answers (1)

chepner
chepner

Reputation: 531798

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 ...]

Upvotes: 3

Related Questions