I am running...
xargs -0 somescript -C << EOF
drush ev 'somethingelse("$STRING",[]);'
EOF
somescript is a script I need to run and I do not control. It expects a command as an argument after -C much like su -c does.
This works if $STRING does not contain an apostrophe but if it does then it falls apart as it can be expected. I tried
xargs -0 somescript -C << "EOF1"
xargs -0 drush ev << 'EOF2'
somethingelse("$STRING",[]);
EOF2
EOF1
but this gives me sh: syntax error: unexpected "(". I do not quite understand why.
Ps.: Please do not recommend running something else: the problem here is running this script. This is not an X-Y problem, I have a screw I need to screw in. I can't use a rivet. I am asking where to find the appropriate screwdriver. Thanks!
Using A Python Helper
The use of
shinstead ofbashcomplicates this a bit -- bash can escape arbitrary strings into content that bash itself can evaluate, but it doesn't have an equivalent feature that targets baseline POSIX sh; hence the use of a Python helper below to provide a solution that works on operating systems where sh is provided by ash, dash, or another non-bash shell.You could even do this further:
somescript_cmd=$(quote somescript -C "$drush_cmd")will create asomescript_cmdvariable that can beeval'd to invokesomescript.Bash Only (5.0+, sh compatibility not guaranteed)
If you're curious how to do this without needing Python: if the program you're starting invokes
bashinstead ofsh, or you don't need to support operating systems whereshis provided by a non-bash shell, one could instead use:Bash Only (3.x-compatible, sh compatibility not guaranteed)
If we avoid using 5.0+-only features, this would instead look like: