I’m trying to make a system call to SSH to a server and echo a string. The string is defined by ruby and has dollars signs in it.
#!/usr/bin/env ruby
string = '$$%%$#@%$#@%325dg345345'
puts %x(ssh server "echo \'#{string}\'")
When I execute, the output that comes back is not the string. The remote server is interpreting the dollar signs as variable. Is there any form of clever escaping I can do to prevent this? The characters are random, so just editing the string to escape the dollar signs will not work.
You can use the stdlib’s Shellwords library to escape not only the dollar signs, but also spaces and any other “dangerous” characters your string might have: