The prompt setup was once one of the most important features of shell programs long time ago and now people barely remember that we can change the colors. lol
The way you can change the color varies depending on what shell program you are using. You can check which shell program you are using by opening a file, /etc/passwd. The line is separated by colons, :. The first column shows the ID and the last column shows which shell program the ID is using. If other shell programs are install, you can change it with a command, "chsh", but bash (Bourne-Again SHell) is good enough so I am not gonna bother.
You can see what's the current prompt setting is by typing "echo $PS1" or "echo $PS2". PS2 is for the case when the command line is multiple lines, which you will barely see.
The default prompt setting of RaspberryPi is little complicated. This setup is stored in ~/.bashrc and if you modify the file, your change will become permanent; in other words, unless you modify the file, ~/.bashrc, your prompt will go back to where it was whenever you login again.
Let's start with a simple example: "export PS1="\u@\h \t \W\$ ".
The human unreadable cryptic code is explained in the manual page of bash: "man bash"
- "\u" means the user name of the current user
- "\h" means the hostname up to the first '.'
- "\t" means the current time in 24-hour HH:MM:SS format
- "\W" means the basename of the current working directory, with $HOME abbreviated with a tilde.
Now it does what I wanted; there is no blue colored characters that I cannot read. But I am also losing the fancy green colored id and hostname. So the coloring is gone.
Coloring is little more tricky. The syntax is like this:
- "EscapeCharacter + [ + color number + m" : this will change the color and the color will be applied to later characters.
- "EscapeCharacter + [ + m" : this will bring the color back to normal.
- There are two ways to type-in "EscapeCharacter": \033 or \e.
- More than one color numbers can be listed with separation mark, ";".
- Color numbers for characters are from 30 to 37: black(30), red(31), green(32), yellow(33), blue(34), magenta(35), cyan(36), white(37).
- Background colors are from 40 to 47.
- Color number 01 will make the color brighter.
I want to apply it every time I login. In order to change the prompt permanently I need to change the file, ~/.bashrc. The screenshot above shows that I commented out the original prompt setting with a mark "#" and I added a new prompt setting.
Now when I login, the prompt is changed. Also the current path name is readable. Well... It says it is time to sleep. lol
*Note that I don't type-in password nor ID. lol. You can do it with one of my previous articles.
There are more tricks with ANSI escape character such as remembering and changing the position of the cursor. When I was kid, I had 4DOS with a fancy prompt. The prompt always showed current time on the top of right side and the memory usage on the top of left side with fancier colors. I loved it. lol
ReplyDeleteIt seems that the color changing code needs to be enclosed with \[ and \] as described here:
ReplyDeletehttps://stackoverflow.com/questions/2024884/commandline-overwrites-itself-when-the-commands-get-too-long
I know you wrote this 5 years ago but it helped me in 2019. Personally i like: "\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[31m\]\u@\h\[\033[00m\]:\[\033[33m\]\w #\[\033[00m\] "
ReplyDelete