I need to execute the shell command as follows:
ssh <device> "command"
command is invoked as:
$(typeset); <function_name> \"arguement_string\"; cd ...; ls ...
How exactly to quote here? Is this correct?
""$(typeset); <function_name> \"arguement_string\""; cd ...; ls ..."
I am confused with this quoting in shell scripts.
I would use a here document:
Note that I wrapped the starting
EOF
in single quotes. Doing so prevents bash from interpreting variables or command substitutions in the local shell.Don't try to do the quoting by hand -- ask the shell to do it for you!
You can then build up
command_array
as you wish -- using logic to conditionally append values, with only the kind of quoting you'd usually refer to use to those values, and letprintf %q
add all additional quoting needed to make the content safe to pass through ssh.If you're trying to incrementally build up a script, you can do that like so:
Note that in this case, all expansions take place locally, when the script is being generated, and shell constructs such as redirection operators cannot be used (except by embedding them in a function you then pass to the remote system with
typeset
). Doing so means that no data passed tosafe_append_command
can be treated as code -- foreclosing large classes of potential security holes at the cost of flexibility.