Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an icon to the bash prompt

I know you can edit the bash prompt permanently by editing the PS1 variable in your ~/.bashrc file, mine looks like this:

PS1="\[\e[0;31m\]<HERP(._.)DERP>\[\e[0;0m\]";

but can you set a tiny little image in there as well? For instance, if I wanted to add a little American flag icon, or something before "HERP(._.)DERP", could I do this?

like image 727
kjh Avatar asked Nov 19 '12 18:11

kjh


2 Answers

Actually, Yes, you can.

In recent versions of Bash, at least 4 (i could do it in 4.2 and 4.3), you can render emoji with the hex.

Use the echo -e flag.

paste an emoji you looked up in and do a hexdump to see what it's made of:

plasmarob ~ $ echo -n "🇺🇸"| hexdump

0000000 f0 9f 87 ba f0 9f 87 b8                        
0000008

And then take that top line and escape each hex pair with \x :

plasmarob ~ $ echo -e 'See? \xf0\x9f\x87\xba\xf0\x9f\x87\xb8'
See? 🇺🇸

I actually modified mine to be:

plasmarob ~ âš¡

So yes, come up with one like this and try adding it to your .bashrc or .bash_profile.

Edit: Something with SO or browser rendering may have changed because the flag in this post now renders as a "US" character. YMMV but i assume it will still work in the stated versions of bash.

like image 164
Plasmarob Avatar answered Sep 28 '22 05:09

Plasmarob


Nowadays, you can add emoji if you have an emoji-aware font. I guess this wasn't a easily viable option when the question was originally posted

I wrote this blog post about it a couple of years ago.

I don't know about American flags, but export PS1="\360\237\232\251 > " gets a flag in your prompt.

I also wrote a shell tool to make printing the escapes for echo or shell prompt a little easier. It's called emo

like image 29
cms Avatar answered Sep 28 '22 04:09

cms