2

Please try:

bash -c "printf '\u2744'"
bash -c "echo -e '\u2744'"

And so:

sh -c "printf '\u2744'"
sh -c "echo -e '\u2744'"

Why in /bin/bash I can get that unicode character but not in /bin/sh? How to print a unicode characer (for example ) in /bin/sh?

Valerio Bozz
  • 1,176
  • 16
  • 32
  • 1
    Because the POSIX sh standard doesn't include Unicode support, so `/bin/sh` isn't required to support it. (Also, the POSIX sh standard doesn't include `echo -e` at all -- one of the few places where bash doesn't just extend the standard, but also breaks it). – Charles Duffy Feb 16 '15 at 15:32
  • 1
    ...see http://pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html for full notes on `echo`; the APPLICATION USAGE and RATIONALE sections are particularly to the point. – Charles Duffy Feb 16 '15 at 15:33
  • As for the POSIX spec for `printf` -- laying out the only behavior `/bin/sh` is required to provide -- it's specified at http://pubs.opengroup.org/onlinepubs/009604599/utilities/printf.html; you'll see no `\u` there either. – Charles Duffy Feb 16 '15 at 15:34

2 Answers2

7

If you want to print a unicode character in /bin/sh (which isn't required by POSIX to support \u sequences), either embed it literally in your script or break it down into its component bytes and print those individually.

For the snowflake symbol used in the question:

printf '\342\235\204'

Now, how do you find those numbers? The easiest way is to use od from a shell that does support unicode:

$ printf '\u2744' | od -b
0000000 342 235 204
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Or just use whatever encoding your terminal supports, and be nonportable to other encodings, assuming your `sh` repeats your strings faithfully. These days, hardcoding UTF-8 encoding might not be a big deal. – tripleee Feb 16 '15 at 16:08
4

Because bash only got support for those in the 4.2 release series.

From https://tiswww.case.edu/php/chet/bash/CHANGES:

------------------------------------------------------------------------------
This document details the changes between this version, bash-4.2-alpha,
and the previous version, bash-4.1-release.
....
3.  New Features in Bash
....
d.  $'...', echo, and printf understand \uXXXX and \UXXXXXXXX escape sequences.
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • Was the question substantially revised since initial post? This is explicitly a question about `/bin/sh`, only mentioning bash for comparison. – Charles Duffy May 04 '20 at 20:05