Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find information on bash special parameters ($* $@ $# $? $- $$ $! $0 $_)? [duplicate]

(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?

like image 623
Keith Thompson Avatar asked Nov 28 '13 17:11

Keith Thompson


1 Answers

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.
like image 128
Keith Thompson Avatar answered Oct 01 '22 01:10

Keith Thompson