Passing all arguments in zsh function
19,017
Use $@
, it expands to all the positional arguments, e.g.:
superfind () {
echo "Errors are suppressed!"
find "$@" 2> /dev/null
}
![Mathias Begert](https://i.stack.imgur.com/CFHTT.jpg?s=256&g=1)
Comments
-
Mathias Begert about 2 years
I am trying to write a simple function in my .zshrc that hides all the errors (mostly "Permission denied") for
find
.Now, how can I pass all the arguments given by calling the function to
find
?function superfind() { echo "Errors are suppressed!" find $(some magic here) 2>/dev/null }
I could do
$1 $2 $3 $4 ...
but this is stupid! I am sure there is a really simple way. -
Ray Andrews almost 9 yearssupposing you wanted to pass all but the first argument?
-
Thor almost 9 years@rayandrews: add a
shift
command before thefind
command. -
Ray Andrews almost 9 yearsI mean in a situation like this: "echo "first arg is: $1 and the remaining args are: $2 $3 $4 $5 ..." ... you can't shift inside the echo. But knowing zsh, I'll bet there is a way.
-
Thor almost 9 years@rayandrews: not sure what you mean, but you can index into an array like this:
echo $a[2,-1]
to get all but the first element.$@
can also be treated as an array. -
Ray Andrews almost 9 yearsThanks Thor, that's it.
-
smac89 over 3 years@RayAndrews In zsh, you can supply an offset to arrays like
${@:1}
. This will skip the first element of the array and give you the rest. Another example:${fpath:1}
. You can also do offset + size like${@:1:3}
will skip the first element and give you the next three