Meaning of dollar sign variables in bash script (with examples)
$1,$2,$3, … are the positional parameters. e.g.
# cat ./script.sh
echo first param: $1
echo second param: $2
echo third param: $3
# bash ./script.sh a b c
# first param: a
# second param: b
# third param: c$@: an array-like construct of all positional parameters, {$1,$2,$3, …}. e.g.
# cat ./script.sh
echo $@
# bash ./script.sh a b c
# a b c$*is the IFS (Internal Field Separator) expansion of all positional parameters,$1,$2,$3, …. It looks similar to but is still different from$@depending on the special shell variable$IFSand whether$*is double quoted or not. See here for more details. e.g.
# cat ./script.sh
echo $*
echo "$*"
IFS='|'
echo $*
echo "$*"
IFS='#'
echo "$*"
# bash ./script.sh a b c
# a b c
# a b c
# a b c
# a|b|c
# a#b#c$#is the number of positional parameters. e.g.
# cat ./script.sh
echo $#
# bash ./script.sh a b c
# 3$-current options set for the shell. See this post for some common shell options available, and this post for more details on$-. e.g.
# cat ./script.sh
set -e
echo $-
# bash ./script.sh
# eHb
# Hb appears to be default.- $$ pid of the current shell (not subshell)
# cat ./script.sh
echo $$
# bash ./script.sh
# 71061
# can be some other number, as well.- $_ most recent parameter (or the absolute path of the command to start the current shell immediately after startup)
# cat ./script.sh
echo $_
# ls -l
# bash ./script.sh
# -l
# because -l is the most recent parameter used, I don't know why this is useful?!-
$IFSthe (input) field separator, see how it’s used together with$*as described above -
$?most recent foreground pipeline exit status. -
$!PID of the most recent background command -
$0name of the shell or shell script
# cat this_awesome_script.sh
echo $0
# $bash this_awesome_script.sh
# this_awesome_script.shReference: http://stackoverflow.com/questions/5163144/what-are-the-special-dollar-sign-shell-variables