0

I am trying to run a command-line remotely that contains some single, double quotes.

1) This is the command I want to have it running on remote host.

  echo '{"id":12345,"name":"activate_cluster"}' 

which should be of the exactly same format. Not any missing characters.

2) This is the full command I have used to trigger this command from my local host:

expect bashscript $hostname $user $pwd 'echo \'\{\"id\":12345\,\"name\":\"activate_cluster\"\}\'

3) But when it reaches the remote host, this command becomes,

echo {"id":12345,"name":"activate_cluster"}

The pair of single quotes is gone! Is there a way I can fix this?

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
user3595231
  • 711
  • 12
  • 29
  • Sorry, for the 2), mine is: => expect bashscript $hostname $user $pwd 'echo \'\{\"id\":12345\,\"name\":\"activate_cluster\"\}\'', there is an extra quote at the end. – user3595231 Feb 02 '16 at 00:10
  • Possible duplicate of [Preserve Quotes in bash arguments](http://stackoverflow.com/questions/10835933/preserve-quotes-in-bash-arguments) – Ken Y-N Feb 02 '16 at 00:17

1 Answers1

1

You cannot embed a single quote within single quotes in bash: https://www.gnu.org/software/bash/manual/bashref.html#Single-Quotes

You'll have to do something like this:

expect bashscript $hostname $user $pwd 'echo '\''{"id":12345,"name":"activate_cluster"}'\'
# ...........................................^^^^......................................^^^

'\'' -- the first quote ends the opening quote from 'echo, the escaped quote appends a literal quote, and the third one opens a new quoted string. Within single quotes, you don't need to go nuts with backslashes -- they're all literal characters in there.

Another approach would be to store the command in a separate variable:

cmd=$(cat <<'END'
echo '{"id":12345,"name":"activate_cluster"}' 
END
)
expect bashscript $hostname $user $pwd "$cmd"

A little wordier but much tidier, no?

glenn jackman
  • 238,783
  • 38
  • 220
  • 352