Consider the following shell script, where POD is set to the name of a K8 pod.
kubectl exec -it $POD -c messenger -- bash -c "echo '$@'"
When I run this script with one argument, it works fine.
hq6:bot hqin$ ./Test.sh x
x
When I run it with two arguments, it blows up.
hq6:bot hqin$ ./Test.sh x y
y': -c: line 0: unexpected EOF while looking for matching `''
y': -c: line 1: syntax error: unexpected end of file
I suspect that something is wrong with how the arguments are passed.
How might I fix this so that arguments are expanded literally by my shell and then passed in as literals to the bash running in kubectl exec?
Note that removing the single quotes results in an output of x only.
Note also that I need the bash -c so I can eventually pass in file redirection: https://stackoverflow.com/a/49189635/391161.
"$@"in double quotes expands to each positional argument in turn as a separate double-quoted command parameter, whereas"$*"expands to a single double-quoted command parameter. It is not defined what happens when there is other stuff in the double quotes, but it turns out that with positional parametersxandy,"echo '$@'"gives you command parameters "echo 'x" and "y'". So don't do that. (Note that the expansion happens on the local machine, the bash command running in the pod isn't getting any positional parameters).