How to pass all arguments passed to my bash script

2019-01-12 13:06发布

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

How can I pass all arguments my bash script has received to it? The number of params is variable, so I can't just hardcode the arguments passed like this:

abc $1 $2 $3 $4

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

7条回答
We Are One
2楼-- · 2019-01-12 14:08

I needed a variation on this, which I expect will be useful to others:

function diffs() {
        diff "${@:3}" <(sort "$1") <(sort "$2")
}

The "${@:3}" part means all the members of the array starting at 3. So this function implements a sorted diff by passing the first two arguments to diff through sort and then passing all other arguments to diff, so you can call it similarly to diff:

diffs file1 file2 [other diff args, e.g. -y]
查看更多
登录 后发表回答