In Bash, there appear to be several variables which hold special, consistently-meaning values. For instance,
./myprogram &; echo $!
will return the PID of the process which backgrounded myprogram
. I know of others, such as $?
which I think is the current TTY. Are there others?
$'\n'
is a literal newline character that result from replacing the digraph \n
with ASCII 10.
${1}
, ${*}
, etc, the braces are just for disambiguation, and often redundant. In isolation, ${x}
is exactly equivalent to $x
.
$IFS
see What is the exact meaning of IFS=$'\n'
$1, $2, $3, ... are the positional parameters.
"$@" is an array-like construct of all positional parameters, {$1, $2, $3 ...}.
"$*" is the IFS expansion of all positional parameters, $1 $2 $3 ....
$# is the number of positional parameters.
$- current options set for the shell.
$$ pid of the current shell (not subshell).
$_ most recent parameter (or the abs path of the command to start the current shell immediately after startup).
$IFS is the (input) field separator.
$? is the most recent foreground pipeline exit status.
$! is the PID of the most recent background command.
$0 is the name of the shell or shell script.
Most of the above can be found under Special Parameters in the Bash Reference Manual. There are all the environment variables set by the shell.
For a comprehensive index, please see the Reference Manual Variable Index.
$_ last argument of last command
$# number of arguments passed to current script
$* / $@ list of arguments passed to script as string / delimited list
off the top of my head. Google for bash special variables.
$@
is the string and $*
is the delimited list (according to the above accepted answer, anyways).
"$@"
expands to a list, "$*"
expands to a single string. The special behavior of $@
applies when it's within double quotes.
To help understand what do $#
, $0
and $1
, ..., $n
do, I use this script:
#!/bin/bash
for ((i=0; i<=$#; i++)); do
echo "parameter $i --> ${!i}"
done
Running it returns a representative output:
$ ./myparams.sh "hello" "how are you" "i am fine"
parameter 0 --> myparams.sh
parameter 1 --> hello
parameter 2 --> how are you
parameter 3 --> i am fine
Take care with some of the examples; $0 may include some leading path as well as the name of the program. Eg save this two line script as ./mytry.sh and the execute it.
#!/bin/bash
echo "parameter 0 --> $0" ; exit 0
Output:
parameter 0 --> ./mytry.sh
This is on a current (year 2016) version of Bash, via Slackware 14.2
Success story sharing
bash
man page. The only oddity is that$_
is only mentioned in the context of its use in theMAILPATH
variable.man(1) bash
under Special Parameters for the rest of the definition of$_
.!$
instead of$_
in bash scripts, because the latter sometimes fails.!$
and$_
.