Search code examples
unixsshraspberry-piraspbian

Passing a variable with escaping double quotes over ssh


If I use a variable with escaping double quotes, ", they get removed over ssh.

Here's an example:

$ foo=\"word\"
$ echo $foo
"word"
$ ssh pi@$pi03 "echo $foo"
word

Does anyone know why that might be/what the solution could be? I could post the code to the practical use I need the quotes for, but figured this was a simpler way of explaining.


Solution

  • Use triple escaping:

    foo=\\\"word\\"
    echo $foo
    \"word\"
    ssh pi@1pi "echo $foo"
    "word"
    

    But in general passing variables this way is something you should avoid.