【问题标题】:What are the special dollar sign variables in zsh?zsh 中有哪些特殊的美元符号变量?
【发布时间】:2020-04-12 13:55:32
【问题描述】:

使用bash 时,有一些自引用变量在zsh 中似乎不可用。比如$?获取最近的前台进程的退出状态,$_获取上一条命令的最后一个参数等

zsh 中是否有与这些等价物?我参考this question 询问 bash。

【问题讨论】:

  • 这是一个很好的起点:Variables Index, Parameters Set By The Shell。我认为$argv[1] 相当于$1$_ 是一样的,但我不是zsh 专家。
  • $1, $2 存在于zsh。它们是 POSIX 所要求的。
  • @Bamar 好点,我改了例子
  • $? 也应该可以工作。 zsh 应该实现所有标准 POSIX 变量。
  • $_ 也是如此,尽管 POSIX 不需要。

标签: bash environment-variables zsh dollar-sign


【解决方案1】:

bash 称这些为特殊参数(以区别于普通变量和位置参数)。 zsh 实现了基本相同的集合,但将它们与 shell 设置的其他参数一起记录下来,尽管它也将每个参数标记为特殊的。

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

【讨论】:

    猜你喜欢
    • 2011-07-06
    • 1970-01-01
    • 2011-12-17
    • 2012-07-31
    • 2012-09-05
    • 1970-01-01
    相关资源
    最近更新 更多