Avoid Wrapping Script Code In Quotes #134
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The exec() command only runs a program with literal argument strings.
It does not know how to do things like expanding environment variables,
or piping/redirection. Hence the way to get "shell intelligence"
is to use exec() to run the
sh
process with the command line stringas a parameter.
But the method being used to do this was:
This has problems, because wrapping the script in quotes creates
issues when the script itself contains quotes. Escaping the string
correctly is a non-trivial problem, which also would create "noise"
in the debug output which would add confusion.
http://mywiki.wooledge.org/BashFAQ/050
The easiest way to work around this here is to use the array form of
exec() to pass exactly two parameters to
sh
. No manipulation of thescript string is needed with this approach:
There are more cases in the project of
sh -c
usage which should alsobe changed, and likely abstracted (shellExec()?) But this small patch
just fixes the most important case for the user-provided script.
Additionally, this removes the escaped backslash (
\\
) from thestart of the executed command. That was presumably to suppress the
use of aliases:
https://unix.stackexchange.com/questions/524254/why-are-backslashes-included-in-this-shell-script
But because this is invoking a non-interactive shell session, aliases
would not apply. Hence the extra backslash shouldn't be needed.