Score:0

Using UTF-16 syntax to include Unicode characters in Bash prompt (PS1)?

vn flag

There is an existing post on Unix & Linux about including Unicode characters in the Bash prompt, but the method it gives for using the UTF-16 code (syntax \uXXXX) doesn't work for me.

Let's take this arrow as an example:

Unicode Character 'BLACK RIGHTWARDS ARROWHEAD' (U+27A4)

enter image description here

As I can see, there should be 3 ways to do Unicode characters in the Bash PS1 prompt:

  • UTF-8 Octal value - in this case: \342\236\244
  • UTF-8 HEX value - in this case: \xe2\x9e\xa4
  • UTF-16 HEX value (combined) - in this case: \u27A4

However, only the first option works in my Ubuntu prompt, as indicated in the image below:

enter image description here

Explanation:

  • With PS1='[\u@\h \W]\$ \342\236\244 ', the Unicode character is applied correctly.
  • With PS1='[\u@\h \W]\$ \xe2\x9e\xa4 ', the exact string is applied instead.
  • With PS1='[\u@\h \W]\$ \u27A4 ', it applies my username (am) and then the exact value 27A4.

I understand that \u is a Bash shortcut for username, which is why I'm confused the other answer states that this works - for me it doesn't.

Is there a way to make the UTF-16 syntax work for PS1 on Ubuntu (since this is both shorter and easier to read)? I'm on Ubuntu 22.04.1 Server.

Bodo avatar
pt flag
Did you notice the `$'...'` in the other answer, e.g. `PS1=$'\u2234\u2192\u263f\u2605'`?
Artur Meinild avatar
vn flag
Thanks for the hint - I think I'm working it out.
Score:1
my flag

If you want to use \xHH or \uHHHH bash sequences in your pretty prompt, use a $ before first simple quote like this:

PS1=$'[\\u@\\h \\W]$ \xe2\x9e\xa4 '

or

PS1=$'[\\u@\\h \\W]$ \u27A4 '

Of course, with simple quote, you don't need \ in before $, but with $'...' definition, it's better to put two \\ before each PS1 special fields (u, h, W in your case)

Artur Meinild avatar
vn flag
Thanks - I missed this detail, because the default Ubuntu prompt starts with: `PS1='${debian_chroot: ...` And I used a simplified example in my question - but it works now!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.