3 ms·
> Replace \ with \\, ' with \', and wrap each argument in 's if it contains anything suspicious. This is not a correct way to do shell quoting. The code doesn
by jwilk 7y ago
> Replace \ with \\, ' with \', and wrap each argument in 's if it contains anything suspicious.
This is not a correct way to do shell quoting.
The code doesn't quite follow this description, but it's not correct either:
$ tmp=$(printf '\nX')
$ newline=${tmp%X}
$ printf '['; quote "$newline"; printf ']\n'
[
]
$ quote 'foo+\bar'
'foo+\\bar'
- kanox 7y agoThe easiest way to quote for the shell: * Add ' at the beginning and end * Replace internal ' with '\'' This is because inside '-quoted string only ' itself has special semantics. > wrap each argument in 's if it contains anything suspicious. Such vague advice is wrong in all contexts.
- pizza234 7y agoI wouldn't rely on a manual mean, considering that there is a built-in API: printf '%q' <string_to_quote>