how to get the last argument passed to a shell script in zsh shell?
$ example.zsh arg1 arg2 ... arglast
last argument is "arglast"
how to get the last argument passed to a shell script in zsh shell?
$ example.zsh arg1 arg2 ... arglast
last argument is "arglast"
@ works mostly¹ like a variable containing the array of positional parameters: $@ can take an array subscript. The last element is at position $# since $# is the number of arguments².
printf 'Last argument is "%s"\n' "${@[$#]}"
Alternatively, in an array subscript, negative values count from the end, so [-1] takes the last element.
printf 'Last argument is "%s"\n' "$@[-1]"
Another way to get the last argument is to use the P parameter expansion flag which does parameter lookup twice: ${(P)foo} takes the value of foo as another parameter name and expands to the value of that. Use this on # which works like a variable that contains the number of positional parameters. Beware however that this only works if there is at least one positional parameter, otherwise you get $0 (the name of the current script). Using the @ array doesn't have this problem.
printf 'Last argument is "%s"\n' "${(P)#}"
¹ The difference is that $@ has an implied @ flag in parameter expansion, so that "$@" and "$@[1,3]" expand to multiple words, like "$array[@]" and "${(@)array[1,3]}".
² Zsh counts both positional parameters and array elements from 1 (unless the ksh_arrays compatibility option is on, in which case array elements count from 0 and the last element would be ${@[${#}-1]}).