custom command prompt
I spend a lot of time in the terminal and at the command prompt. The default prompt for Ubuntu is very…unhelpful, to say the least. I already know who I am and most of the time I know what machine I am on. So after talking to a friend of mine and doing a little research I have come up with this custom command prompt that suits me just fine.
The first part is the time that the prompt was given. This idea came from my friend. I like it for things like figuring out just how long that last command took (without having to run it again with the time command) and for simply seeing what time it is (since I am in the terminal a lot).
The next part is the current directory I am working in. I made it a different color to make it stand out a bit more from the rest of the stuff on the screen. So, with a quick glance, I can determine if I am in the right place before running the command.
The last part is the coolest (in my opinion) by far. I found an example of the smiley faces being used and really liked that idea. Once again at a glance I can determine something important. This time, I can determine whether or not the last command was successful. If I see red, I can look back and see what happened. If no red, keep going, everything is just fine. Pretty cool.
Now to the nitty gritty. How exactly do you create a custom prompt like this. After ripping things from a few different places (and doing a lot of my own troubleshooting) I came up with this for my custom command prompt:
PS1="[\e[00m][\T] [\e[1;34m]\w `if [ \$? = 0 ]; then echo -e '[\e[01;32m]:)'; else echo -e '[\e[01;31m]:('; fi` [\033[00m]\$[\e[00m] "
Confused?
I was too. In fact, I am still unsure as to what exactly some of it does. Oh well.
The segments that look like [e[00m] (sometimes with different numbers) are specifying the colors of the different sections. The first part you can see that after the color specification section there is a [\T]. This simply specifies that the current time (\T) needs to be printed between some square brackets ([]).
Next there are some more colors specified followed by \w. This specifies that I want the current working directory displayed. A capital W will display the current working directory without the path.
Then comes the fun stuff. Bash scripting within a string! Basically it says that if the last command was all good, display a smiley face. However, if something went wrong, a frowning face is displayed (in red). The bash scripting is just a simple if-else statement with some echo calls.
The key here is making sure everything is escaped correctly. Add a comment if you have any questions. BTW, here is another good reference for this type of thing.


