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,261
  • 7
  • 48
  • 95
Bart van Heukelom
  • 41,958
  • 59
  • 182
  • 293

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
  • 446,329
  • 63
  • 468
  • 610
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
  • 141
  • 3
  • 9