You aren't appending “multiple variables”. You're appending a string. The most straightforward interface for your function would be to call it with a single string as its argument:
myfunction 'test1 test2'
and then (not using function as the function name because it's a keyword in some shells)
myfunction () {
sudo sed -i "/^STRING/ s/\$/ $1/" FILE
}
Beware that this doesn't actually append the specified string, because the argument is interpolated into the sed script. Any / or \ in the argument is not interpreted literally. You can work around that by first preparing the argument:
myfunction () {
extra=$(printf '%s\n' "$1" | sed 's![\\/]!\\&!g')
sudo sed -i "/^STRING/ s/\$/ $extra/" FILE
}
Alternatively you can use another tool such as perl:
myfunction () {
sudo env extra="$1" perl -i -pe '/^STRING/ s/$/\Q$ENV{extra}/' FILE
}
If you absolutely want your function to take a series of strings as arguments, and append them all in order with a space in between, then you can use "$*". This expands to a single word containing all the arguments of the function with a space in between.
myfunction () {
extra=$(printf '%s\n' "$*" | sed 's![\\/]!\\&!g')
sudo sed -i "/^STRING/ s/\$/ $extra/" FILE
}
XXXXandYYYY?set -xbefore that...$@expands to the positional parameters (and also word splits as it's not quoted) which effectively translates tosed 's/$/ $1' $2 ... $n/ FILEhence the error. Try something likeadd_user () { s=$@; sed "s/$/$s/" FILE; }