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
}
Share:
19,017
Mathias Begert
Author by

Mathias Begert

I am not you.

Updated on June 03, 2022

Comments

  • Mathias Begert
    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
    Ray Andrews almost 9 years
    supposing you wanted to pass all but the first argument?
  • Thor
    Thor almost 9 years
    @rayandrews: add a shift command before the find command.
  • Ray Andrews
    Ray Andrews almost 9 years
    I 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
    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
    Ray Andrews almost 9 years
    Thanks Thor, that's it.
  • smac89
    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