How do I write non-ASCII characters using echo?
-
19-08-2019 - |
Question
How do I write non-ASCII characters using echo? Is there an escape sequence, such as \012
or something like that?
I want to append ASCII characters to a file using:
echo ?? >> file
Solution
Use
echo -e "\012"
OTHER TIPS
If you care about portability, you'll drop echo and use printf(1):
printf '\012'
On my terminal,
printf '\012' >>output.txt
works for both the octal representation of the ascii character, and the corresponding hexadecimal:
printf '\xA' >>output.txt
The command
echo -en '\012' >>output.txt
however, does not function properly. Only hexadecimals seem to work with echo -e. The -n removes the default extra newline from echo.
I took non-ASCII to mean Unicode, at least in my case, but printf "\x##"
wasn't enough for my 2-byte solution, so I used this slightly different syntax instead:
> printf "\u25ba"
►
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow