Is there any comprehensive list of characters that need to be escaped in Bash? Can it be checked just with sed
?
In particular, I was checking whether %
needs to be escaped or not. I tried
echo "h%h" | sed 's/%/i/g'
and worked fine, without escaping %
. Does it mean %
does not need to be escaped? Was this a good way to check the necessity?
And more general: are they the same characters to escape in shell
and bash
?
Characters that need escaping are different in Bourne or POSIX shell than Bash. Generally (very) Bash is a superset of those shells, so anything you escape in
shell
should be escaped in Bash.A nice general rule would be "if in doubt, escape it". But escaping some characters gives them a special meaning, like
\n
. These are listed in theman bash
pages underQuoting
andecho
.Other than that, escape any character that is not alphanumeric, it is safer. I don't know of a single definitive list.
The man pages list them all somewhere, but not in one place. Learn the language, that is the way to be sure.
One that has caught me out is
!
. This is a special character (history expansion) in Bash (and csh) but not in Korn shell. Evenecho "Hello world!"
gives problems. Using single-quotes, as usual, removes the special meaning.Using the
print '%q'
technique, we can run a loop to find out which characters are special:It gives this output:
Some of the results, like
,
look a little suspicious. Would be interesting to get @CharlesDuffy's inputs on this.format that can be reused as shell input
There is a special
printf
format directive (%q
) built for this kind of request:Some samples:
This could be used through variables too:
Quick check with all (128) ascii bytes:
Note that all bytes from 128 to 255 have to be escaped.
This must render something like:
Where first field is hexa value of byte, second contain
E
if character need to be escaped and third field show escaped presentation of character.Why
,
?You could see some characters that don't always need to be escaped, like
,
,}
and{
.So not always but sometime:
or
but care:
There are two easy and safe rules which work not only in
sh
but alsobash
.1. Put the whole string in single quotes
This works for all chars except single quote itself. To escape the single quote, close the quoting before it, insert the single quote, and re-open the quoting.
sed command:
sed -e "s/'/'\\\\''/g; 1s/^/'/; \$s/\$/'/"
2. Escape every char with a backslash
This works for all characters except newline. For newline characters use single or double quotes. Empty strings must still be handled - replace with
""
sed command:
sed -e 's/./\\&/g; 1{$s/^$/""/}; 1!s/^/"/; $!s/$/"/'
.2b. More readable version of 2
There's an easy safe set of characters, like
[a-zA-Z0-9,._+:@%/-]
, which can be left unescaped to keep it more readablesed command:
LC_ALL=C sed -e 's/[^a-zA-Z0-9,._+@%/-]/\\&/g; 1{$s/^$/""/}; 1!s/^/"/; $!s/$/"/'
.Note that in a sed program, one can't know whether the last line of input ends with a newline byte (except when it's empty). That's why both above sed commands assume it does not. You can add a quoted newline manually.
Note that shell variables are only defined for text in the POSIX sense. Processing binary data is not defined. For the implementations that matter, binary works with the exception of NUL bytes (because variables are implemented with C strings, and meant to be used as C strings, namely program arguments), but you should switch to a "binary" locale such as latin1.
(You can easily validate the rules by reading the POSIX spec for
sh
. For bash, check the reference manual linked by @AustinPhillips)To save someone else from having to RTFM... in bash:
...so if you escape those (and the quote itself, of course) you're probably okay.
If you take a more conservative 'when in doubt, escape it' approach, it should be possible to avoid getting instead characters with special meaning by not escaping identifier characters (i.e. ASCII letters, numbers, or '_'). It's very unlikely these would ever (i.e. in some weird POSIX-ish shell) have special meaning and thus need to be escaped.
I presume that you're talking about bash strings. There are different types of strings which have a different set of requirements for escaping. eg. Single quotes strings are different from double quoted strings.
The best reference is the Quoting section of the bash manual.
It explains which characters needs escaping. Note that some characters may need escaping depending on which options are enabled such as history expansion.