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.
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.
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.
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.
Use $@
, it expands to all the positional arguments, e.g.:
superfind () {
echo "Errors are suppressed!"
find "$@" 2> /dev/null
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With