39

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
flybywire
  • 261,858
  • 191
  • 397
  • 503

6 Answers6

38

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

printf '\012'
Nietzche-jou
  • 14,415
  • 4
  • 34
  • 45
  • 5
    ...or, if taking the escape sequence from an untrusted source, `printf '%b' '\012'`, to honor such sequences but not other format string contents. – Charles Duffy May 20 '16 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 '19 at 00:13
33

Use

echo -e "\012"
Himanshu
  • 31,810
  • 31
  • 111
  • 133
vartec
  • 131,205
  • 36
  • 218
  • 244
  • 5
    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. – Charles Duffy May 20 '16 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 '19 at 00: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.

mareoraft
  • 3,474
  • 4
  • 26
  • 62
  • 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 09 '14 at 03: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 '19 at 00: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](https://stackoverflow.com/a/65878758/4561887) and [here](https://stackoverflow.com/questions/1250079/how-to-escape-single-quotes-within-single-quoted-strings/65878993#65878993). Granted, my examples are primarily for single (`'`) and double (`"`) quotes, which are ASCII chars. – Gabriel Staples Jan 25 '21 at 04:44
8

You can use ANSI-C Quoting with echo:

echo $'\012' # octal

echo $'\x0a' # hex
Yuri
  • 4,254
  • 1
  • 29
  • 46
wisbucky
  • 33,218
  • 10
  • 150
  • 101
  • 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 '20 at 05: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"
►
Pysis
  • 1,452
  • 2
  • 17
  • 31
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
Tinmarino
  • 3,693
  • 24
  • 33