3

I have this function in a Bash script:

comp() {
    rsync -v --archive $1/ $TMP/$2 $3 $4 $5 $6 $7 $8 $9
}

As you can see, I'm doing something special with arguments $1 and $2. Then I hackily just append all the rest of them to the end of the command. They go to $9, but in fact all should be appended.

There must be an easier way for this?

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301

2 Answers2

5

You can use substring expansion, which might be useful in certain situations. For this, though, I must say I prefer Brian's solution of shifting, as it is a bit clearer. (Also, Brian's solution is POSIX; substring expansion is a bash extension.)

comp () {

    rsync -v --archive "$1"/ "$TMP/$2" "${@:3}"

}
chepner
  • 497,756
  • 71
  • 530
  • 681
4

I wouldn't necessarily call it "easier," but you can do this:

comp() {
    archive=$1
    tempfile=$2
    shift 2
    rsync -v --archive $archive/ $TMP/$tempfile "$@"
}

That saves you from having to hard-code $3 through $11.

Brian
  • 151
  • 3
  • 9