I want to create a bash function that will accept variable number of parameters. There must be at least one parameter. Then I call another program and need to insert fixed values between the first parameter accepted and the rest of parameters.
I do it based on this answer
tt() {
name="$1"
params="${@:2}"
someapp ${name} 1234 /some/path "${params}"
}
But it doesn't work as intended as if I call it
tt John has fun
the resulting call will be
someapp John 1234 /some/path 'has fun'
note quotes, while I need the result to be
someapp John 1234 /some/path has fun
with two last args not in common quotes.
$@
should always be quoted! Without quotes, the shell would split the strings in the argument list up into more strings on spaces, tabs, and newlines and perform filename globbing on those new strings. This essentially stops you from passing arguments containing spaces (or at least doing it correctly, preserving the spaces). Please also read the answers to What is the difference between $* and $@? – Kusalananda Jan 12 '23 at 15:42