De aqui :
$# Stores the number of command-line arguments that
were passed to the shell program.
$? Stores the exit value of the last command that was
executed.
$0 Stores the first word of the entered command (the
name of the shell program).
$* Stores all the arguments that were entered on the
command line ($1 $2 ...).
"$@" Stores all the arguments that were entered
on the command line, individually quoted ("$1" "$2" ...).
Então, basicamente, $#
é um número de argumentos dados quando seu script foi executado. $*
é uma string contendo todos os argumentos. Por exemplo, $1
é o primeiro argumento e assim por diante. Isso é útil se você quiser acessar um argumento específico no seu script.
Como Brian comentou, aqui está um exemplo simples. Se você executar o seguinte comando:
./command -yes -no /home/username
-
$#
= 3 -
$*
=-yes -no /home/username
-
$@
= array:{"-yes", "-no", "/home/username"}
-
$0
=./command
,$1
=-yes
etc.
Eles fazem parte do padrão POSIX e devem ser compatíveis com todos os shells compatíveis . Para a referência, abaixo estão as definições padrão POSIX para cada parâmetro especial. Observe que há três variáveis adicionais: $-
, $$
e $!
.
$@
:
Expands to the positional parameters, starting from one. When the expansion occurs within double-quotes, and where field splitting (see Field Splitting) is performed, each positional parameter shall expand as a separate field, with the provision that the expansion of the first parameter shall still be joined with the beginning part of the original word (assuming that the expanded parameter was embedded within a word), and the expansion of the last parameter shall still be joined with the last part of the original word. If there are no positional parameters, the expansion of '@' shall generate zero fields, even when '@' is double-quoted.
$*
:
Expands to the positional parameters, starting from one. When the expansion occurs within a double-quoted string (see Double-Quotes), it shall expand to a single field with the value of each parameter separated by the first character of the IFS variable, or by a if IFS is unset. If IFS is set to a null string, this is not equivalent to unsetting it; its first character does not exist, so the parameter values are concatenated.
$#
:
Expands to the decimal number of positional parameters. The command name (parameter 0) shall not be counted in the number given by '#' because it is a special parameter, not a positional parameter.
$?
:
Expands to the decimal exit status of the most recent pipeline (see Pipelines).
$-
:
(Hyphen.) Expands to the current option flags (the single-letter option names concatenated into a string) as specified on invocation, by the set special built-in command, or implicitly by the shell.
$$
:
Expands to the decimal process ID of the invoked shell. In a subshell (see Shell Execution Environment ), '$' shall expand to the same value as that of the current shell.
$!
:
Expands to the decimal process ID of the most recent background command (see Lists) executed from the current shell. (For example, background commands executed from subshells do not affect the value of "$!" in the current shell environment.) For a pipeline, the process ID is that of the last command in the pipeline.
$0
:
(Zero.) Expands to the name of the shell or shell script. See sh for a detailed description of how this name is derived.