How do I find information on bash special paramete

2019-02-09 21:45发布

This question already has an answer here:

(I've seen a number of questions here about Bash special parameters. It can be difficult to search for things like $*, both in the Bash manual and via Google. This question is intended to be a general reference for these questions.)

The Bash shell defines a number of "special parameters" (which is itself a bit confusing, since most of us think of them as "variables", not "parameters"). References to them consist of a dollar sign followed by some punctuation character.

Google searches for strings consisting of punctuation characters are notoriously difficult, and there are no occurrences of, for example, $? in the Bash Reference Manual.

How can I find information on particular Bash special parameters?

1条回答
Explosion°爆炸
2楼-- · 2019-02-09 22:11

Documentation on Bash special parameters:

$* $@ $# $? $- $$ $! $0 $_

can be found in the Bash Reference Manual, specifically in section 3.4.2, "Special Parameters". If you have the bash documentation installed on your system, you can type

% info bash

and then search for "Special Parameters".

As rici points out in a comment, within the info command you can also find the special parameters via the index: type i and then type the single character (excluding the $), then Enter. This doesn't work for ?, and searching for ! finds a different section first (typing , to find the next entry works). (This still works reasonably well after I apply my patch.)

It's unfortunate, IMHO, that this section refers to these parameters without the leading $ character. (I've just submitted a patch that changes this.)

A brief summary (but read the manual for details):

  • $*: Expands to the positional parameters starting with $1.
  • $@: Also expands to the positional parameters, but behaves differently when enclosed in double quotes.
  • $#: Expands to the number of positional parameters in decimal.
  • $?: Expands to the exit status of the most recent command. (Similar to $status in csh and tcsh.)
  • $-: Expands to the current option flags.
  • $!: Expands to the process ID of the most recent background command.
  • $0: Expands to the name of the shell or script. (Note that $0, unlike $1 et al, is not a positional parameter.)
  • $_: Initially set to the absolute pathname use to invoke the shell or shell script, later set to the last argument of the previous command. (There's more; see the manual.)

UPDATE :

As of bash version 4.3, released 2014-02-26, the bash documentation is annotated to show the full names of these variables. In release 4.2:

`#'
     Expands to the number of positional parameters in decimal.

In release 4.3:

`#'
     ($#) Expands to the number of positional parameters in decimal.
查看更多
登录 后发表回答