3 ms·
Adding double quotes is risky with regard to "`", "$" and other special shell characters. Adding single quotes is safer, but will fail if the argument itself c
by vog 12y ago
Adding double quotes is risky with regard to "`", "$" and other special shell characters.
Adding single quotes is safer, but will fail if the argument itself contains single quotes.
To my knowledge, the correct shell escaping is to surround your argument with single quotes, and before doing that, replace each contained single quote
'
with the following sequence (1):
'\''
Alternatively, you can use (2):
'"'"'
Both sequences work the same way: First, terminate the single-quoted string. Then, add an escaped single quote, either with backslash (1) or by encapsulating into double quotes (2). Finally, start an new single-quoted string.
- Tiksi 12y agoAs far as I know, escaping single quotes inside single quotes doesn't work in bash: tiksi@emperor:~$ curl 'http://\'test' > > > >^C tiksi@emperor:~$ curl 'http://\'test'' curl: (6) Could not resolve host: \test tiksi@emperor:~$ Anything inside single quotes is treated literally, so no escapes work.( Unless you're doing $'.stuffhere', but that's a whole other can of worms). The double quotes around single quotes does work though.
- vog 12y ago> escaping single quotes inside single quotes That's why you have to escape single quotes with '\'' rather than just \' As I said, you first have to terminate the single-quoted string, then add the single quote, then start a new single-quoted string. In your example, my variant (1) leads to: curl 'http://'\''test' And my variant (2) leads to: curl 'http://'"'"'test'
- Tiksi 12y agoAh, I misread your first post as meaning '...\'...' instead of literally '\''. I've always used the '"'"' method, or "$(sed -r 's/([allthespecialchars])/\\\1/g')" , but '\'' is much cleaner. I have found the terminate and escape quite useful for color codes though 'string'\e[31m'red'"$var"'morestring', not sure how I never thought to use it for '\'', but thats getting added to my toolbox :)