Article pages now have a discussion option at the bottom (moderated/captcha, but no registration needed)

Special parameters and shell variables

Special Parameters

parametercharacterexpansion description
*asteriskThe positional parameters starting from the first. When used inside doublequotes (see quoting), like "$*", it expands to all positional parameters as one word, delimited by the first character if the IFS variable (a space in this example): "$1 $2 $3 $4".
If IFS is unset, the delimiter used will be always a space, if IFS is NULL, the delimiter will be nothing, which effectively concatenates all the positional parameters without any delimiter.
When used unquoted, it will just expand to the strings, one by one, not preserving the word boundaries (i.e. word splitting will split the text again, if it contains IFS characters.
See also the scripting article about handling positional parameters.
@at-signThe positional parameters starting from the first. When used inside doublequotes (see quoting), like "$@", it expands all positional parameters as separate words: "$1" "$2" "$3" "$4"
Without doublequotes, the behaviour is like the one of * without doublequotes.
See also the scripting article about handling positional parameters.
#hash markNumber of positional parameters (decimal)
See also the scripting article about handling positional parameters.
?question markStatus of the most recently executed foreground-pipeline (exit/return code)
-dashCurrent option flags set by the shell itself, on invocation, or using the set builtin command. It's just a set of characters, like himB for h, i, m and B.
$dollar-signThe process ID (PID) of the shell. In an explicit subshell it expands to the PID of the current "main shell", not the subshell.
!exclamation markThe process ID (PID) of the most recently executed background pipeline (like started with command &)
0zeroThe name of the shell or the shell script (filename). Set by the shell itself.
If Bash is started with a filename to execute (script), it's set to this filename. If started with the -c <CMDLINE> option (commandline given as argument), then $0 will be the first argument after the given <CMDLINE>. Otherwise, it is set to the string given on invocation for argv[0].
Unlike popular belief, $0 is not a positional parameter.
_underscoreA kind of catch-all parameter. Directly after shell invocation, it's set to the filename used to invoke Bash. Subsequently, expands to the last argument to the previous command. Placed into the environment when executing commands, and set to the full pathname of these commands. When checking mail, this parameter holds the name of the mail file currently being checked.

Shell Variables

Not all, only the most interesting ones. If you think somethin's missing here - feel free to extend the table at any time.

IFSThe Internal Field Separator, used to split input into separate fields after reading with the read command. Also used to split fields in various other situations, like mass-populating an array.
IGNOREEOFAn integer that indicates how much EOF characters the shell must receive (in a row) to finally exit. If it exists and is empty or non-integer, the default of 10 is assumed. If it's unset, every EOF exits the shell.
PIPESTATUSAn array indicating the exit code of every part of a pipeline construct. The normal exit code $? would only reflect the status of the pipeline as a whole. From a construct cat foo | grep bar, PIPESTATUS[0] would be the cat command, PIPESTATUS[1] would be the grep.
SECONDSEach time referenced, expands to the number of seconds since shell startup. Once unset, it looses its special meaning.
SHELLOPTSExpands to a : (colon) separated list of shell options set (with the set builtin command) using their long option names
RANDOMEach time referenced, it expands to a value between 0 and 32767. The random generator may be re-initialized by assigning a value to it. Once unset, it looses its special meaning, even is re-set. To get a random number from 0 to a specific maximum, use the modulo arithmetic operation: RANDOM % 100
BASHPID:V4: Always expands to the process ID (PID) of the current used shell (this is different from $$!)
PROMPT_DIRTRIM:V4: If set to a positive integer, the expansion of the current directory in the PS1 prompt will be trimmed with "..." if more than PROMPT_DIRTRIM pathname components would be shown

Discussion

Enter your comment
 
syntax/shellvars.txt · Last modified: 2009/04/14 20:38 by thebonsai
GNU Free Documentation License 1.2
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0