简体   繁体   中英

Replace a part of a string in zsh

So, in my zshrc I have a blue prompt, and when the root user is used, it uses a version of the same prompt with the word blue changed to red

if [ $UID = 0 ]
then
     export PS1="%B%F{red}[ %n@%m ]%f%b %F{white}:%f %F{yellow}%~%f %B%F{cyan}>%b%f "
     export RPS1="%B%F{cyan}<%b%f "$(date +"%d/%m/%y ~ %H:%M:%S")""
else 
     export PS1="%B%F{blue}[ %n@%m ]%f%b %F{white}:%f %F{yellow}%~%f %B%F{cyan}>%b%f "
     export RPS1="%B%F{cyan}<%b%f "$(date +"%d/%m/%y ~ %H:%M:%S")""
fi

I've done it like this for some time, but is it possible to just replace the word blue in PS1="%B%F{blue}..... with red? Or, the other way around?

Use a separate parameter to store you choice of color, then use that in the definition of PS1 .

if [ $UID = 0 ]; then
    color=red
else
    color=blue
fi

PS1="%B%F{}[ %n@%m ]%f%b %F{white}:%f %F{yellow}%~%f %B%F{cyan}>%b%f "
RPS1="%B%F{cyan}<%b%f %D{%d/%m/%y ~ %T}"  # You don't need to call date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM