I prefer a minimal bash prompt. Recently, however, I saw an oh-my-zsh prompt, that I thought would be useful.
The intriguing prompt displayed a symbol indicating whether the previous command exited successful (exit code zero) or failed (non-zero exit code).
You can always get the exit code of the previous command from the $?
variable, but seeing it right there and in color, is more direct.
While I find fish
andzsh
quite intriguing, I am still unwilling to move my setup for just a single feature. “This has to be possible in bash,” I thought… And it is, though the implementation was a bit more complex than I expected. But I learned a lot more about how the bash prompt worked.
The trick for changeable or dynamic prompts in bash is to create a bash function that assembles the PS1
variable, on every prompt. You can enable that function by setting the PROMPT_COMMAND
environment variable to your custom function.
Obviously, you should not overload your function with time intensive processes, but with modern processing power, a lot can be done in a short time.
After a lot of experimentation, I settled on this setup:
Update: there was an error in the code that would prevent the prompt from ever showing a red exit code. I fixed it now, the change is in the last line. (Thanks to co-worker Mattias for pointing that out.)
You can add this code to your .bash_profile
or .bashrc
. (If you do not know what that means, read this post.)

I experimented with special characters and even Emoji to signify the exit code, but then settled on colors and the square root symbol √
(option-V on the US and international keyboard, looks like a checkmark) for success and the question mark ?
with the exit code for errors.
Obviously, you can use a modified prompt command to show all kinds of other statuses as well. Enjoy!