5

I'm trying to customize my Bash prompt with Unicode characters and I'm having a bit of trouble. If I try to include a Unicode character like this:

$ echo ☢ | hexdump -C
00000000  e2 98 a2 0a                                       |....|
00000004

$ PS1="\xe2\x98\xa2\x0a"

I see my prompt appears like this:

\xe2\x98\xa2\x0a

...rather than showing the ☢ character. What am I doing wrong?

9

That's not valid bash escape syntax for simple double quotes. Try instead

PS1=$'\xe2\x98\xa2 '

This uses the special $'...' quoting that bash has and which does support ANSI C escapes. Note that the 0a is just linefeed (newline character) and I doubt you want that, so I took the liberty of replacing it with a space above.

Of course, instead of going through the entire rigamarole of finding the UTF-8 encoding, and then typing out the escapes, you could just use the raw character itself (PS1='☢ ') and it would still work.

  • 1
    *facepalm* why didn't I think of just trying the character!? – Naftuli Kay Dec 20 '12 at 23:17
2

With recent versions of bash, you can also use the Unicode value:

PS1=$'\u2622 '

That way, it will get expanded/displayed properly (or at least not as bad) even if the local charset/encoding is not utf-8 (or it will cause bash to crash or output rubbish as I think I've uncovered a bug there).

  • Very nice. I was wondering if/why this feature wasn't present. Somehow, I feel better about including \u2622 than a raw Unicode character . – Naftuli Kay Dec 21 '12 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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