bashunicodeshchars

Unicode characters in /bin/sh VS /bin/bash


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?


Solution

  • 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