I have strings containing percent-escaped characters like %20
and %5B
, and I would like to transform it to "normal" characters like \
for %20
and [
for %5B
.
How can I do that?
A non-quoted backslash, \, is used as an escape character in Bash. It preserves the literal value of the next character that follows, with the exception of newline.
To escape a string for use as a command line argument in Bash, simply put a backslash in front of every non-alphanumeric character. Do not wrap the string in single quotes or double quotes.
The escape (\) preceding a character tells the shell to interpret that character literally. With certain commands and utilities, such as echo and sed, escaping a character may have the opposite effect - it can toggle on a special meaning for that character.
Bash Character Escaping. Except within single quotes, characters with special meanings in Bash have to be escaped to preserve their literal values. In practice, this is mainly done with the escape character \ <backslash>.
The builtin printf in bash has a special format specifier (i.e. %b) which converts \x** to the corresponding value:
$ str='foo%20%5B12%5D'
$ printf "%b\n" "${str//%/\\x}"
foo [12]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With