Artificial intelligent assistant

How do I prevent $@ from clashing with double quote in bash? The following example returns "a b" instead of expected "a b c": test() { bash -c "testargs() { echo \$@; }; testargs $@"; } test "a b" c It seems it's a quoting issue. How do I solve it without using "$*" instead?

`$@` should pretty much only be used in the form `"$@"`, alone in a word. When `"stuff$@"` is expanded, that results in a list consisting of the first positional parameter with `stuff` prepended, then the other positional parameters. For example, if the positional parameters are `foo` and `bar`, you get two words: `stufffoo` and `bar`. That's one of your problems.

Another problem is that with `sh -c`, the first non-option argument becomes the script name which is available as `$0`. Subsequent non-option arguments become positional parameters. So `sh -c 'echo "$@"' foo bar qux` prints `bar qux` — `foo` is in `$0`, `bar` in `$1` and `qux` in`$2`.

To pass the arguments of the function to the script, use `$@` in the script (not, like you did, in the function — `"stuff$@"` is expanded in the shell that executes the function, due to the double quotes). And account for `$0`.


test() { bash -c 'testargs() { echo "$@"; }; testargs "$@"' myscript "$@"; }

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy f9acdcb70214d0faff643a7668dc6a04