Converting a zsh prompt to bash

Dan Nelson dnelson at allantgroup.com
Wed May 17 08:04:59 PDT 2006


In the last episode (May 17), Kyrre Nygard said:
> Do you think this would work?
> 
> I tried applying your principles, as well as some information design:
> 
> local a1="01;36m"
> local a2="22;36m"
> local a3="01;30m"
> 
> local b1="01;31m"
> local b2="22;31m"
> local b3="01;30m"
> 
> PROMPT=$'%{$a1}(%{$a2}%n%{$a3}@%{$a2}%m%{$a1})'
> PROMPT+=$'%{$a1}('{$a2}%D{%H:%M}%{$a3}+%{$a2}%D{%d/%m}%{$a1})%{$a3}\n'
> PROMPT+=$'%{$a1}(%{$a2}%#%{$a3}:%{$a2}%~%{$a1})'
> 
> if [[ `whoami` = root ]] then
> PROMPT=$'%{$b1}(%{$b2}%n%{$b3}@%{$b2}%m%{$b1})'
> PROMPT+=$'%{$b1}(%{$b2}%D{%H:%M}%{$b3}+%{$b2}%D{%d/%m}%{$b1})%{$b3}\n'
> PROMPT+=$'%{$b1}(%{$b2}%#%{$b3}:%{$b2}%~%{$b1})'
> fi

Note that zsh provides symbolic variables for color setting:

 autoload -U colors
 colors
 echo "$fg[blue]$bg[red]blue on red!"

so you don't have to memorize the numbers.  See the zshcontrib manpage,
"OTHER FUNCTIONS" section.

If the only difference between your root prompt is color, you can also
just set a1,a2,a3 to different values within your if block, then set
PROMPT outside of it.

 if [[ $USER == root ]] ; then
  a1="%{$fg[cyan]$bg[black]}"
 else
  a1="%{$fg[red]}$bg[black]}"
 fi
 PROMPT="$a1>"


-- 
	Dan Nelson
	dnelson at allantgroup.com


More information about the freebsd-questions mailing list