Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

Customize zsh's prompt when displaying previous command exit code

Zsh includes the ability to display the return code/exit code of the previous command in the prompt by using the %? escape sequence.

However I would like to have the following prompt:

user@host ~ [%?] %

when the exit code is different from 0 and:

user@host ~ %

when exit code is 0.

If I use %? alone it is always displayed, even if %? is 0. In addition I want the square brackets but only when the exit code not 0.

What is the simplest way to do this?

question from:https://stackoverflow.com/questions/4466245/customize-zshs-prompt-when-displaying-previous-command-exit-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Add this in the position in PS1 where you want the exit code to appear:

%(?..[%?] )

It's a conditional expression. The part between the two dots (nothing in this case) is output if the expression before the first dot is true. The part after the second dot is output if it's false.

For example:

PS1='%n%m %~ %(?..[%?] )%# '

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...