[Melbourne-pm] Bash prompt red on error

Sam Watkins sam at nipl.net
Wed May 12 21:37:44 PDT 2010


On Thu, May 13, 2010 at 01:44:33PM +1000, Hamish Carpenter wrote:
> As briefly discussed last night, I mentioned I'd changed my prompt to
> be red on error (return code != 0). This helps me detect failed
> commands and also helps in another case where you type a command then
> cancel it by pressing ctrl+c. This was what prompted me to change my
> prompt as someone watching your terminal cannot easily tell that
> command was canceled, especially if it is a command that would have no
> output on success (eg rf -f project_directory).
> 
> add the following to your .bash_profile:
> 
> # needs PS1 to be previously defined so add nearer the end of your
> .bashrc/.bash_profile
> # will turn entire prompt red (31) or green (32)
> # depending on previous output return code
> export PS1="\[\e[\`if [ \$? = 0 ]; then echo '0;32m'; else echo
> '0;31m'; fi\`\]$PS1\[\e[0m\]"

cool, a friend showed me something similar, giving
^_^ for a success prompt, and O_o for failure :)

	# SmiliePrompt - from http://x-demon.org/
	smiley() { if [ $? == 0 ]; then echo '^_^'; else echo '0_o'; fi; }
	export PS1='$(smiley) '

It's important to know exit status, so I use this all the time.  The shell
should really do something like this by default, maybe not the cute emoticon
version though.

enjoy!  :)


More information about the Melbourne-pm mailing list