Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing all arguments in zsh function

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.

like image 646
Mathias Begert Avatar asked Aug 31 '15 05:08

Mathias Begert


People also ask

How do you assign a all the arguments to a single variable?

Assigning the arguments to a regular variable (as in args="$@" ) mashes all the arguments together like "$*" does. If you want to store the arguments in a variable, use an array with args=("$@") (the parentheses make it an array), and then reference them as e.g. "${args[0]}" etc.

How do you pass arguments to a shell script?

To invoke a function, simply use the function name as a command. To pass parameters to the function, add space-separated arguments like other commands. The passed parameters can be accessed inside the function using the standard positional variables i.e. $0, $1, $2, $3, etc.

What is bash $@?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


1 Answers

Use $@, it expands to all the positional arguments, e.g.:

superfind () {
    echo "Errors are suppressed!"
    find "$@" 2> /dev/null
}
like image 114
Thor Avatar answered Nov 15 '22 17:11

Thor