In Bash, when specifying command line arguments to a command, what characters are required to be escaped?
Are they limited to the metacharacters of Bash: space, tab, ‘|’, ‘&’, ‘;’, ‘(’, ‘)’, ‘<’, or ‘>’?
The following characters have special meaning to the shell itself in some contexts and may need to be escaped in arguments:
Some of those characters are used for more things and in more places than the one I linked.
There are a few corner cases that are explicitly optional:
Escaping a newline requires quoting — backslashes won't do the job. Any other characters listed in IFS will need similar handling. You don't need to escape
Some of these characters have tighter limits on when they truly need escaping than others. For example,
If your command name itself is a shell keyword (
1Stéphane has noted that any other single-byte blank character from your locale also needs escaping. In most common, sensible locales, at least those based on C or UTF-8, it's only the whitespace characters above. In some ISO-8859-1 locales, U+00A0 no-break space is considered blank, including Solaris, the BSDs, and OS X (I think incorrectly). If you're dealing with an arbitrary unknown locale, it could include just about anything, including letters, so good luck.
Conceivably, a single byte considered blank could appear within a multi-byte character that wasn't blank, and you'd have no way to escape that other than putting the whole thing in quotes. This isn't a theoretical concern: in an ISO-8859-1 locale from above, that
I think this behaviour is broken multiple ways, but we have to play the hand we're dealt. If you're working with any non-self-synchronising multibyte character set, the safest thing would be to quote everything. If you're in UTF-8 or C, you're safe (for the moment).