7

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

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 1
    Given as this was clearly created with the intent that it be a reference question, it's curious to see it closed as a duplicate. – Charles Duffy Jan 23 '18 at 06:22

1 Answers1

10

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.
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 2
    If you have the `bash` manpage, which is probably installed by default, you can `man bash` and search for Special Parameters. If you have the `info` file installed (`bash-doc` package on some distros), you can search for the individual special character in the index, using the `i` command; eg `i*`. This works for almost all the special parameters. (`info bash '--index-search=*'` is possible but too much typing imho.) – rici Nov 28 '13 at 18:02
  • @rici: Sure -- *if* you already know that "Special Parameters" is the term used by the manual. Personally, I tend to think of them as "variables", not "parameters". Good point about the index; I didn't know that trick. – Keith Thompson Nov 28 '13 at 18:06
  • I was just quoting your answer about searching "Special Parameters"; it's just as easy in the manpage as the info doc. One of the many idiosyncracies of `info` is that if you want the index entry (starting with) `?`, you need to type the question mark twice, because the first `?` is the 'show-completions' character, subtly different from `tab`. – rici Nov 28 '13 at 18:31
  • Submitted a patch for bash? Could you please ensure that someone generates the online docs & updates those. – devnull Nov 28 '13 at 19:18
  • 1
    @devnull: If the patch is accepted, presumably that will happen more or less automatically. If not, I'll make the patch available for anyone who wants to apply it and build from source. – Keith Thompson Nov 28 '13 at 19:24
  • I know at least two people, including me, have submitted a patch to make special parameters searchable in man bash, but it was rejected both times :/ – that other guy Nov 28 '13 at 21:30
  • @thatotherguy: The change was made in release 4.3. I can't tell whether it was done in response to the patch I submitted. – Keith Thompson Apr 05 '18 at 16:18
  • @KeithThompson That's great, though I see that this is only the case for the info page, and not for the man page – that other guy Apr 05 '18 at 16:54