test.sh and run as ./test.sh apple banana:echo "$0" echo "$1" echo "$#" echo "$@" echo "$$"
What will be the output?
echo "$0" echo "$1" echo "$#" echo "$@" echo "$$"
$0 is the script name as called (./test.sh), $1 is the first argument (apple), $# counts arguments (2), $@ lists all arguments separated by spaces (apple banana), and $$ is the process ID (a number).
$? hold?Example:
ls /nonexistent echo $?
ls /nonexistent echo $?
$? tells if the last command worked or not.$? holds the exit status of the last command. Zero means success, any other number means an error or failure.
./script.sh one two three:echo $#
What is the output?
echo $#$# counts how many arguments are given to the script.$# counts the number of arguments passed, excluding the script name. Here, three arguments are passed.
./script.sh a b c:echo "$@" echo "$*"
What is the output?
echo "$@" echo "$*"
$@ and $* behave differently when quoted.When quoted, "$@" expands each argument as a separate quoted string, so it prints each argument separated by spaces. "$*" expands all arguments as a single string separated by the first character of IFS (space by default), so it prints all arguments as one string.
$$ represent?Example:
echo $$
echo $$
$$ is related to the process running the script.$$ holds the process ID (PID) of the current shell running the script. This is useful for creating unique temporary files or tracking the script process.