In bash
use the ANSI C like strings, with the $'...'
notation as below. This is especially useful when you want to pass special characters as arguments to some programs.
myProgram $'foo\nbar'
You can see the hexdump
of the string formed. Don't confuse the trailing new line, since it is introduced by the here-string <<<
construct in bash
$ hexdump -c <<< $'foo\nbar'
0000000 f o o \n b a r \n
0000008
The following escape sequences are also supported, updating the list here, since it is not available in the duplicated one.
+-------------+----------------------------------------------------------------------------------------------------------------------------------+
| code | meaning |
| | |
+-------------+----------------------------------------------------------------------------------------------------------------------------------+
| \" | double-quote |
| \' | single-quote |
| \\ | backslash |
| \a | terminal alert character (bell) |
| \b | backspace |
| \e | escape (ASCII 033) |
| \E | escape (ASCII 033) \E is non-standard |
| \f | form feed |
| \n | newline |
| \r | carriage return |
| \t | horizontal tab |
| \v | vertical tab |
| \cx | a control-x character, for example, $'\cZ' to print the control sequence composed of Ctrl-Z (^Z) |
| \uXXXX | Interprets XXXX as a hexadecimal number and prints the corresponding character from the character set (4 digits) (Bash 4.2-alpha)|
| \UXXXXXXXX | Interprets XXXX as a hexadecimal number and prints the corresponding character from the character set (8 digits) (Bash 4.2-alpha)|
| \nnn | the eight-bit character whose value is the octal value nnn (one to three digits) |
| \xHH | the eight-bit character whose value is the hexadecimal value HH (one or two hex digits) |
+-------------+----------------------------------------------------------------------------------------------------------------------------------+