1

Im trying to escape all the backslashes from some text generated by figlet. Here is what I have so far.

figlet -f $font -t "Foo Bar" | sed -e "s|\\|\\\\|g"

This doesn't work for some reason, but this does.

figlet -f $font -t "Foo Bar" | sed -e "s| |X|g"

Am I misunderstanding how sed works here? I couldn't find similar posts about this either.

1

The double quotes do not protect the backslashes that are inside the sed -e "s|\\|\\\\|g" from the shell's attempts to parse them. If you use a simpler example you can see this a bit more easily:

$ echo 'blah\blah'| sed -e "s|\\|blah|g"
sed: -e expression #1, char 10: unterminated `s' command

$ echo 'blah\blah'| sed -e 's|\\|blah|g'
blahblahblah

If you want to use the first form of your sed you need to switch it to single quotes instead of double.

$ figlet -t "Foo Bar" | sed -e 's|\\|\\\\|g'
 _____             ____
|  ___|__   ___   | __ )  __ _ _ __
| |_ / _ \\ / _ \\  |  _ \\ / _` | '__|
|  _| (_) | (_) | | |_) | (_| | |
|_|  \\___/ \\___/  |____/ \\__,_|_|

If you have to use double quotes, then you need more backslashes to escape the shell and sed:

$ figlet -t "Foo Bar" | sed -e "s|\\\|\\\\\\\|g"
 _____             ____
|  ___|__   ___   | __ )  __ _ _ __
| |_ / _ \\ / _ \\  |  _ \\ / _` | '__|
|  _| (_) | (_) | | |_) | (_| | |
|_|  \\___/ \\___/  |____/ \\__,_|_|
  • Oh wow thats interesting. So everything works fine now that I'm in single quotes. But what if I want to also add environment variables in there? I can't do that with single quotes (I don't need it for what I'm working on, but I'm curious of how to get around this) – Amlesh Sivanantham Aug 24 '18 at 0:10
  • @AmleshSivanantham - you have to escape the backslashes with more if you want to use double quotes. – slm Aug 24 '18 at 0:43

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.