How to pass all arguments passed to my Bash script to a function of mine?

Viewed 459513

Let's say I have a function abc() that will handle the logic related to analyzing the arguments passed to my script.

How can I pass all arguments my Bash script has received to abc()? The number of arguments is variable, so I can't just hard-code the arguments passed like this:

abc $1 $2 $3 $4

Better yet, is there any way for my function to have access to the script arguments' variables?

7 Answers

abc "$@" is generally the correct answer. But I was trying to pass a parameter through to an su command, and no amount of quoting could stop the error su: unrecognized option '--myoption'. What actually worked for me was passing all the arguments as a single string :

abc "$*"

My exact case (I'm sure someone else needs this) was in my .bashrc

# run all aws commands as Jenkins user
aws ()
{
    sudo su jenkins -c "aws $*"
}
Related