41

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

6 Answers 6

40

If you care about portability, you'll drop echo and use printf(1):

printf '\012'
2
  • 5
    ...or, if taking the escape sequence from an untrusted source, printf '%b' '\012', to honor such sequences but not other format string contents. May 20, 2016 at 20:55
  • note that the format is \NNN, not \0.... So if you want Octal 101, use printf '\101', not printf '\0101'
    – wisbucky
    Nov 26, 2019 at 0:13
34

Use

echo -e "\012"
2
  • 6
    This requires functionality that's entirely noncompliant with POSIX (not just an extension, but actively disallowed by the specification, which allows no flags other than -n to be honored), and won't be honored by even bash in POSIX mode if the xpg_echo flag is set. May 20, 2016 at 20:52
  • Note, the format is \0.... So if you want octal 101, you use "\0101". You must have a leading zero with echo -e.
    – wisbucky
    Nov 26, 2019 at 0:19
14

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.

3
  • 2
    My question was actually on how to print hexadecimal to a terminal - found this through google, and this answered my question very well. It also answers the asker's question. It deserves more points.
    – Wyatt Ward
    Aug 9, 2014 at 3:54
  • echo -en '\012' >>output.txt worked perfectly for me on both Ubuntu 18.04 GNU bash v4.4.19 and macOS mojave GNU bash, version 3.2.57. It's a little easier to visualize if you use a visible character like '\0101' (A).
    – wisbucky
    Nov 26, 2019 at 0:17
  • Only hexadecimals seem to work with echo -e. This isn't true, in my experience. See a bunch of my examples using hex, octal, or mixed escape codes here and here. Granted, my examples are primarily for single (') and double (") quotes, which are ASCII chars. Jan 25, 2021 at 4:44
9

You can use ANSI-C Quoting with echo:

echo $'\012' # octal

echo $'\x0a' # hex
1
  • Exactly what I needed, as this works not just in echo, but in mv as well. This in combination with xxd to get the hex values, then I can rename my incorrectly encoded filenames to exactly what they have to be.
    – uldics
    Oct 13, 2020 at 5:45
2

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"
►
2

Brief

echo -e 'toto\010\010ti'   # OUTPUTS: toti
echo -e '\x41'             # OUTPUTS: A
echo -e '\u03B1'           # OUTPUTS: α
echo -e '\U1F413 <= \U1F1EB\U1F1F7' # OUTPUTS 🐓 <= 🇫🇷

Doc

From man bash > /BUILTIN/ > /^ *echo/

              \0nnn  the eight-bit character whose value is the octal value nnn (zero  to
                     three octal digits)
              \xHH   the eight-bit character whose value is the hexadecimal value HH (one
                     or two hex digits)
              \uHHHH the Unicode (ISO/IEC 10646) character whose value is the hexadecimal
                     value HHHH (one to four hex digits)
              \UHHHHHHHH
                     the Unicode (ISO/IEC 10646) character whose value is the hexadecimal
                     value HHHHHHHH (one to eight hex digits)

Link

  • Ascii list: man ascii
  • Unicode list: script on StackOverflow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.