Linux: Bourne Shell Scripting Variables

$0 is the script / program name. If you run echo $0 straight from the shell you probably will get “bash” back.

$@ is an array of arguments. It consists of $1 $2 … $N. The loop counter will return how many arguments you have. When N is more than a single digit, it must be enclosed in braces like ${N}.

$* is a string of arguments. The loop counter will always return 1.

$# return how many arguments passed into the script.

$? return exit status of the previous command.

$$ return the process id of the current script.

$! return the latest background process id.

$- return the parameter of your shell is running with, which you can set with “set” command.

echo $-
# himBHs
set +h
echo $-
# imBHs
set -h
echo $-
# himBHs

$_ return last argument of the previous command.

# To set parameter $1 $2 $3
set 1 "hello world" -f 
set "hello world" -f 1
set -f 1 "hello world"		# (-f is ignored) => $1 = 1, $2 = "hello world"
set - -f 1 "hello world"
set -- -f 1 "hello world"
set -- 						# unset

References:


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *