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:
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:
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 value27A4
.
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.
If you want to use
\xHH
or\uHHHH
bash sequences in your pretty prompt, use a$
before first simple quote like this:or
Of course, with simple quote, you don't need
\
in before$
, but with$'...'
definition, it's better to put two\\
before eachPS1
special fields (u
,h
,W
in your case)