I'm trying to bind Alt-s to an SSH command that is quite complex and requires nested double quotes.

bind '"\es":"ssh -t me@myserver  \"echo \\"hi\\"; echo \\"bye\\"\""'

But when I execute the keybinding half the string is truncated:

ssh -t me@myserver  "echo \"hi\";

How do I get the entire string to be inserted? I don't know if this is a problem with the space after the semi colon, or with all the double-escaping of double-quotes.

Note: I don't want to use bind -x style because that one a) doesn't print the actual command b) executes it without any ability to manually modify the line before executing it by pressing enter.


Escaping for shell strings is tricky. Try:

bind '"\es":"ssh -t localhost  \"echo \\\"hi\\\"; echo \\\"bye\\\"\""'

After the above command is executed, then Esc-s should result in the following text on the command line:

ssh -t localhost  "echo \"hi\"; echo \"bye\""

This will work correctly. When executed, it produces the output:

Connection to localhost closed.

The issue was that \\" is interpreted as a single backslash followed by a shell-active double-quote. You need to escape the double-quote so that it is inactive. Thus \\\".

  • 1
    Seems to work, thank you. I thought I'd tried that but either I was doing it wrong, or my actual command has some other issues. I may ask a followup here if I can't figure the rest out. – user7000 Jul 15 '17 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.