Skip to main content
deleted 23 characters in body
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78
PS1='${?#0}$ ' 

It uses a special form of parameter expansion, ${?#0}, which means: "Remove the character zero if it is the first character of ${?}, the exit code of the previous command."

You can also change the color of the prompt if the last exit code were not zero:

PS1='\[\e[0;$(($?==0?0:91))m\]$ \[\e[0m\]' 

Prompt

That uses a if-else ternary expression $(($?==0?0:91)) — with some escaping — that makes the color code 0;91m (red, see color codes) if the last command exits with non-zero, or 0;0m (your default color) otherwise.

PS1='${?#0}$ ' 

It uses a special form of parameter expansion, ${?#0}, which means: "Remove the character zero if it is the first character of ${?}, the exit code of the previous command."

You can also change the color of the prompt if the last exit code were not zero:

PS1='\[\e[0;$(($?==0?0:91))m\]$ \[\e[0m\]' 

Prompt

That uses a if-else ternary expression $(($?==0?0:91)) — with some escaping — that makes the color code 0;91m (red, see color codes) if the last command exits with non-zero, or 0;0m (your default color) otherwise.

PS1='${?#0}$ ' 

It uses a special form of parameter expansion, ${?#0}, which means: "Remove the character zero if it is the first character of ${?}, the exit code of the previous command."

You can also change the color of the prompt if the last exit code were not zero:

PS1='\[\e[0;$(($?==0?0:91))m\]$ \[\e[0m\]' 

Prompt

That uses a if-else ternary expression $(($?==0?0:91)) that makes the color code 0;91m (red, see color codes) if the last command exits with non-zero, or 0;0m (your default color) otherwise.

There was no point in using double-quotes, it just made the PS1 definition more spooky
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78
PS1="\$PS1='${?#0}\$$ "' 

It uses a special form of parameter expansion, ${?#0}, which means: "Remove the character zero if it is the first character of ${?}, the exit code of the previous command."

You can also change the color of the prompt if the last exit code were not zero:

PS1="\[\e[0;\$PS1='\[\e[0;$((\$$?==0?0:91))m\]\$m\]$ \[\e[0m\]"\[\e[0m\]' 

PromptPrompt

That uses a if-else ternary expression \$$((\$$?==0?0:91)) — with some escaping — that makes the color code 0;91m (red, see color codes) if the last command exits with non-zero, or 0;0m (your default color) otherwise.

PS1="\${?#0}\$ " 

It uses a special form of parameter expansion, ${?#0}, which means: "Remove the character zero if it is the first character of ${?}, the exit code of the previous command."

You can also change the color of the prompt if the last exit code were not zero:

PS1="\[\e[0;\$((\$?==0?0:91))m\]\$ \[\e[0m\]" 

Prompt

That uses a if-else ternary expression \$((\$?==0?0:91)) — with some escaping — that makes the color code 0;91m (red, see color codes) if the last command exits with non-zero, or 0;0m (your default color) otherwise.

PS1='${?#0}$ ' 

It uses a special form of parameter expansion, ${?#0}, which means: "Remove the character zero if it is the first character of ${?}, the exit code of the previous command."

You can also change the color of the prompt if the last exit code were not zero:

PS1='\[\e[0;$(($?==0?0:91))m\]$ \[\e[0m\]' 

Prompt

That uses a if-else ternary expression $(($?==0?0:91)) — with some escaping — that makes the color code 0;91m (red, see color codes) if the last command exits with non-zero, or 0;0m (your default color) otherwise.

A ternary expression is more clear and customizable than string substitution
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78

That's a great idea.

PS1="\${?#0}\$ " 

It uses a special form of parameter expansion, ${?#0}, which means: "Remove the character zero if it is the first character of ${?}, the exit code of the previous command."

ThingsYou can get pretty cool by changingalso change the color of the prompt if the last exit code were not zero:

PS1="\[\e[1;\${PS1="\[\e[0;\$((\$?/#[!0]*/31}==0?0:91))m\]\$ \[\e[0m\]" 

ThePrompt

That uses a if-else ternary expression \$((\$?==0?0:91)) — with some escaping — that makes the color code is 1;310;91m (red, see color codes here) if the last command exits with non-zero, becauseor ${?/#[!0]*/31}0;0m substitutes a initial 0 with 31(your default color) otherwise.

enter image description here

That's a great idea.

PS1="\${?#0}\$ " 

It uses a special form of parameter expansion, ${?#0}, which means: "Remove the character zero if it is the first character of ${?}, the exit code of the previous command."

Things can get pretty cool by changing the color of the prompt if the last exit code were not zero:

PS1="\[\e[1;\${?/#[!0]*/31}m\]\$ \[\e[0m\]" 

The color code is 1;31 (red, see color codes here) if the last command exits with non-zero, because ${?/#[!0]*/31} substitutes a initial 0 with 31.

enter image description here

PS1="\${?#0}\$ " 

It uses a special form of parameter expansion, ${?#0}, which means: "Remove the character zero if it is the first character of ${?}, the exit code of the previous command."

You can also change the color of the prompt if the last exit code were not zero:

PS1="\[\e[0;\$((\$?==0?0:91))m\]\$ \[\e[0m\]" 

Prompt

That uses a if-else ternary expression \$((\$?==0?0:91)) — with some escaping — that makes the color code 0;91m (red, see color codes) if the last command exits with non-zero, or 0;0m (your default color) otherwise.

Added color changing!
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78
Loading
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78
Loading