Search code examples
bashshellprompt

Messed up bash prompt, probably due to changes in bashrc


I wanted to change my bash prompt (like any nerd) and keep a custom name for it.

I have already come up with the code which works, but it has some problems. The code is

if [ "`id -u`" -eq 0 ]; then
    PS1="[ \[\e[1;33m\]| Dragon Master | \[\e[1;36m\]\[\e[49m\] \W \[\e[0m\]]\e[0;37m ";
else
    PS1="\[\e[1;36m\][ \[\e[1;33m\]| Dragon Master | \W $\[\e[1;36m\]]\e[0;37m ";
fi

And it works fine (not totally),

The demo

But there is a problem with this, when the command gets bigger then one line, it does not go to the next line, but it starts overwriting in the same line. Like this,

Another demo

For reference, the command in the above picture is

x86_64-softmmu/qemu-system-x86_64 -m 1024 -enable-kvm -drive if=virtio,file=test.qcow2,cache=none -cdrom Fedora-Live-Desktop-x86_64-20-1.iso

Now, this weird line wrapping does not effect the command in any way. It works perfectly.

Suggestion Guys?


Solution

  • Found the answer in the link that was posted in the comments.

    In PS1 quote, every color set must have a \\[ preceding it.

    So, my code becomes,

    if [ "`id -u`" -eq 0 ]; then
        PS1="[ \\[\e[1;33m\]| Dragon Master | \\[\e[1;36m\]\\[\e[49m\] \W \\[\e[0m\]]\\[\e[0;37m\] ";
    else
        PS1="\\[\e[1;36m\][ \\[\e[1;33m\]| Dragon Master | \W $\\[\e[1;36m\]]\\[\e[0;37m\] ";
    fi
    

    Taken from this answer.