Skip to main content
deleted 15 characters in body
Source Link
ilkkachu
  • 147.8k
  • 16
  • 268
  • 441

I started tweaking around with my bash prompt lately and I find myself not understanding how the escape character works. I have the following:

PS1="\[$RED\]\342\224\214\342\224\200"

In this I get it, \[\[ escapes the [[ character and \xxx\xxx escapes my UTF-8 characters. But in the following line I get a weird result:

PS1+="$([·\$?·!=·0·]·&&·echo·\[$RED\]\342\234\227\·)"

This will always print XX in my prompt, yet if I escape the first $$ it will print it only when exit status of any command is non zero. I do not understand why. Wasn't $(commands)$(commands) supposed to output the result of given commandscommands? If I escape it like so \$()\$() is the whole sequence escaped or just the dollar sign? If I don't escape why doesn't it print $$? It just prints the XX. I have the same question for the $$ inside the square brackets. Why do I have to escape it?

Also I believe this qualifies as another question but is there any way of printing the actual exit status in my prompt?

I started tweaking around with my bash prompt lately and I find myself not understanding how the escape character works. I have the following:

PS1="\[$RED\]\342\224\214\342\224\200"

In this I get it, \[ escapes the [ character and \xxx escapes my UTF-8 characters. But in the following line I get a weird result:

PS1+="$([·\$?·!=·0·]·&&·echo·\[$RED\]\342\234\227\·)"

This will always print X in my prompt, yet if I escape the first $ it will print it only when exit status of any command is non zero. I do not understand why. Wasn't $(commands) supposed to output the result of given commands? If I escape it like so \$() is the whole sequence escaped or just the dollar sign? If I don't escape why doesn't it print $? It just prints the X. I have the same question for the $ inside the square brackets. Why do I have to escape it?

Also I believe this qualifies as another question but is there any way of printing the actual exit status in my prompt?

I started tweaking around with my bash prompt lately and I find myself not understanding how the escape character works. I have the following:

PS1="\[$RED\]\342\224\214\342\224\200"

In this I get it, \[ escapes the [ character and \xxx escapes my UTF-8 characters. But in the following line I get a weird result:

PS1+="$([·\$?·!=·0·]·&&·echo·\[$RED\]\342\234\227\·)"

This will always print X in my prompt, yet if I escape the first $ it will print it only when exit status of any command is non zero. I do not understand why. Wasn't $(commands) supposed to output the result of given commands? If I escape it like so \$() is the whole sequence escaped or just the dollar sign? If I don't escape why doesn't it print $? It just prints the X. I have the same question for the $ inside the square brackets. Why do I have to escape it?

Also I believe this qualifies as another question but is there any way of printing the actual exit status in my prompt?

Source Link

How does the escape character '\' work in bash prompt?

I started tweaking around with my bash prompt lately and I find myself not understanding how the escape character works. I have the following:

PS1="\[$RED\]\342\224\214\342\224\200"

In this I get it, \[ escapes the [ character and \xxx escapes my UTF-8 characters. But in the following line I get a weird result:

PS1+="$([·\$?·!=·0·]·&&·echo·\[$RED\]\342\234\227\·)"

This will always print X in my prompt, yet if I escape the first $ it will print it only when exit status of any command is non zero. I do not understand why. Wasn't $(commands) supposed to output the result of given commands? If I escape it like so \$() is the whole sequence escaped or just the dollar sign? If I don't escape why doesn't it print $? It just prints the X. I have the same question for the $ inside the square brackets. Why do I have to escape it?

Also I believe this qualifies as another question but is there any way of printing the actual exit status in my prompt?