Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

But in comparasion with MS systems, escaping strings for unix shells is very simple - just prepend and append an ' and change every ' into '\''


Alas, no. Bourne shell syntax allows for double quotes with variable interpolation and some other fancy syntax (including backslash escaping of literal double-quote characters), and a single-quote syntax for "raw" strings with no fancy syntax INCLUDING backslash escaping.

So your rule won't work. You can't single-quote a string that itself contains single quotes, which makes for some fun when you have arbitrary strings (file names are the big frustration) that need to be substituted into a parseable command line.

But like I said above: Bourne syntax[1] is only one kind of insanity, which is still much better than the DOS/Windows world of a separate parser for every app.

[1] We shall not speak of the C shell here.


Their rule does work. The single quotes are transformed into '\''.

The first ' closes the current single quotes string. The \' adds a single quote character outside a quoted string (and outside single quotes you can use backslash escapes). Finally, the last ' reopens the single quoted string that we closed before.


A method I've used: replace all occurrences of ' with '"'"' and then surround resulting string with '

It's a bit ugly but it works.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: