Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

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.

share|improve this answer
1  
*facepalm* why didn't I think of just trying the character!? –  Naftuli Tzvi Kay Dec 20 '12 at 23:17

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).

share|improve this answer
    
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 Tzvi Kay Dec 21 '12 at 17:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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