Você pode usar
$#
veja man bash
para mais dicas de bash
Dada uma função bash:
function f1 {
echo "The function f1 is being called with ??? argument(s)."
}
e duas invocações:
f1 arg1
f1 arg1 arg2
Como posso obter o número de argumentos? Saída desejada:
The function f1 is being called with 1 argument(s).
The function f1 is being called with 2 argument(s).
Usando o argumento especial
$#
que retorna o número de argumentos posicionais dados a esta invocação do shell.
De man bash
...
$1 - $9 these variables are the positional parameters.
$0 the name of the command currently being executed.
$# the number of positional arguments given to this invocation of the shell.
$? the exit status of the last command executed is given as a decimal string. When a command completes successfully, it returns the exit status of 0 (zero), otherwise it returns a non-zero exit status.
$$ the process number of this shell - useful for including in filenames, to make them unique.
$! the process id of the last command run in the background.
$- the current options supplied to this invocation of the shell.
$* a string containing all the arguments to the shell, starting at $1.
$@ same as above, except when quoted.
Esses argumentos especiais são muito úteis para coletar dados sobre o shell atual.
Tags bash