5 added 14 characters in body
source | link

Gilles identified your main problem, but I wanted to try explaining it differently.

Bash interprets the special prompt escapes only before expanding any variables in the prompt. This means that using \e in a variable that is expanded from the prompt doesn't work, even though it does work directly in PS1.

I.e.For example, this works as expected, and gives red text:

PS1='\e[1;31m this is in red '

But this doesn't, it just puts a literal \e in the prompt.:

RED='\e[1;31m'
PS1='$REDPS1="$RED not in red "

If you want to store the color escapes in variables, you can use theANSI-C quoting ($''$'...' quoting) to put a literal escape character in the variable.

To do this, you can change your definition of GREEN, RED, and NONE, so their value is the actual escape sequence.

GREEN=$'\033[1;32m'
RED=$'\033[1;31m'
NONE=$'\033[m'

If you do that, your first PS1 with the single quotes should work:

PS1='${RED}\h $(get_path) ${exitStatus}${NONE} '

However, then you will have a second problem.

Try running that, then press Up Arrow, then Home, and your cursor will not go back to the start of the line.

To fix that, change PS1 to include \[ and \] around the color escape sequences, e.g.

PS1='\[${RED}\]\h $(get_path) $?\[${NONE}\] '

You can't use get_exit_status properly here, since its output contains both printing (the exit code) and non-printing characters (the color codes), and there's no way to mark it correctly in the prompt. Putting \[...\] would mark it as non-printing in full, which is not correct. You'll have to change the function so that it only prints the proper color-code, and then surround it with \[...\] in the prompt.

Gilles identified your main problem, but I wanted to try explaining it differently.

Bash interprets the special prompt escapes only before expanding any variables in the prompt. This means that using \e in a variable that is expanded from the prompt doesn't work, even though it does work directly in PS1.

I.e. this works as expected, and gives red text:

PS1='\e[1;31m this is in red '

But this doesn't, it just puts a literal \e in the prompt.

RED='\e[1;31m'
PS1='$RED not in red "

If you want to store the color escapes in variables, you can use the $'' quoting to put a literal escape character in the variable.

To do this, you can change your definition of GREEN, RED, and NONE, so their value is the actual escape sequence.

GREEN=$'\033[1;32m'
RED=$'\033[1;31m'
NONE=$'\033[m'

If you do that, your first PS1 with the single quotes should work:

PS1='${RED}\h $(get_path) ${exitStatus}${NONE} '

However, then you will have a second problem.

Try running that, then press Up Arrow, then Home, and your cursor will not go back to the start of the line.

To fix that, change PS1 to include \[ and \] around the color escape sequences, e.g.

PS1='\[${RED}\]\h $(get_path) $?\[${NONE}\] '

You can't use get_exit_status properly here, since its output contains both printing (the exit code) and non-printing characters (the color codes), and there's no way to mark it correctly in the prompt. Putting \[...\] would mark it as non-printing in full, which is not correct. You'll have to change the function so that it only prints the proper color-code, and then surround it with \[...\] in the prompt.

Gilles identified your main problem, but I wanted to try explaining it differently.

Bash interprets the special prompt escapes only before expanding any variables in the prompt. This means that using \e in a variable that is expanded from the prompt doesn't work, even though it does work directly in PS1.

For example, this works as expected, and gives red text:

PS1='\e[1;31m this is in red '

But this doesn't, it just puts a literal \e in the prompt:

RED='\e[1;31m'
PS1="$RED not in red "

If you want to store the color escapes in variables, you can use ANSI-C quoting ($'...') to put a literal escape character in the variable.

To do this, you can change your definition of GREEN, RED, and NONE, so their value is the actual escape sequence.

GREEN=$'\033[1;32m'
RED=$'\033[1;31m'
NONE=$'\033[m'

If you do that, your first PS1 with the single quotes should work:

PS1='${RED}\h $(get_path) ${exitStatus}${NONE} '

However, then you will have a second problem.

Try running that, then press Up Arrow, then Home, and your cursor will not go back to the start of the line.

To fix that, change PS1 to include \[ and \] around the color escape sequences, e.g.

PS1='\[${RED}\]\h $(get_path) $?\[${NONE}\] '

You can't use get_exit_status properly here, since its output contains both printing (the exit code) and non-printing characters (the color codes), and there's no way to mark it correctly in the prompt. Putting \[...\] would mark it as non-printing in full, which is not correct. You'll have to change the function so that it only prints the proper color-code, and then surround it with \[...\] in the prompt.

4 The comparison with 'echo' is just wrong. Gilles' answer explains it correctly, but this was missing the point entirely.
source | link

Gilles identified your main problem, but I wanted to try explaining it differently.

Bash is effectively callinginterprets the echo on yourspecial prompt escapes only before expanding any variables in the prompt. This means that using PS1\e in a variable that is expanded from the prompt doesn't work, noteven though it does work directly in echo -ePS1.

So it's like you're doingI.e. this works as expected, and gives red text:

echoPS1='\e[1;31m '\e[1;32m\h\e[m'this is in red '

If you try running thatBut this doesn't, you will see it doesn't work.

But bash gives youjust puts a way to write special characters that doesn't require using echo -e. It looks likeliteral $'\octal number'\e.

The special character in all the escape sequences is \e, which just means Escape. Escape's octal value is \033prompt.

So we want it to expand to this instead:

echoRED='\e[1;31m'
PS1='$RED $'\033[1;31m'"${HOSTNAME}"$'\033[m'not in red "

If you want to store the color escapes in variables, you can use the $'' quoting to put a literal escape character in the variable.

To do this, you can change your definition of GREEN, RED, and NONE, so their value is the actual escape sequence.

GREEN=$'\033[1;32m'
RED=$'\033[1;31m'
NONE=$'\033[m'

If you do that, your first PS1 with the single quotes should work:

PS1='${RED}\h $(get_path) ${exitStatus}${NONE} '

However, then you will have a second problem.

Try running that, then press Up Arrow, then Home, and your cursor will not go back to the start of the line.

To fix that, change PS1 to include \[ and \] around the color escape sequences, e.g.

PS1='\[${RED}\]\h $(get_path) $?\[${exitStatus}\]\[${NONE}\] '

And it should all be working.

(I'm not sure why puttingYou can't use \[get_exit_status aroundproperly here, since its output contains both printing (the exit code) and non-printing characters (the color codes), and there's no way to mark it correctly in the prompt. Putting ${exitStatus}\[...\] workswould mark it as non-printing in full, because the exit status number shouldn'twhich is not correct. You'll have those aroundto change the function so that it only prints the proper color-code, butand then surround it seems to work for mewith \[...\] in the prompt.)

Gilles identified your main problem, but I wanted to try explaining it differently.

Bash is effectively calling echo on your PS1, not echo -e.

So it's like you're doing:

echo '\e[1;32m\h\e[m'

If you try running that, you will see it doesn't work.

But bash gives you a way to write special characters that doesn't require using echo -e. It looks like $'\octal number'.

The special character in all the escape sequences is \e, which just means Escape. Escape's octal value is \033.

So we want it to expand to this instead:

echo $'\033[1;31m'"${HOSTNAME}"$'\033[m'

To do this, you can change your definition of GREEN, RED, and NONE, so their value is the actual escape sequence.

GREEN=$'\033[1;32m'
RED=$'\033[1;31m'
NONE=$'\033[m'

If you do that, your first PS1 with the single quotes should work:

PS1='${RED}\h $(get_path) ${exitStatus}${NONE} '

However, then you will have a second problem.

Try running that, then press Up Arrow, then Home, and your cursor will not go back to the start of the line.

To fix that, change PS1 to include \[ and \] around the color escape sequences, e.g.

PS1='\[${RED}\]\h $(get_path) \[${exitStatus}\]\[${NONE}\] '

And it should all be working.

(I'm not sure why putting \[ around ${exitStatus} works, because the exit status number shouldn't have those around it, but it seems to work for me.)

Gilles identified your main problem, but I wanted to try explaining it differently.

Bash interprets the special prompt escapes only before expanding any variables in the prompt. This means that using \e in a variable that is expanded from the prompt doesn't work, even though it does work directly in PS1.

I.e. this works as expected, and gives red text:

PS1='\e[1;31m this is in red '

But this doesn't, it just puts a literal \e in the prompt.

RED='\e[1;31m'
PS1='$RED not in red "

If you want to store the color escapes in variables, you can use the $'' quoting to put a literal escape character in the variable.

To do this, you can change your definition of GREEN, RED, and NONE, so their value is the actual escape sequence.

GREEN=$'\033[1;32m'
RED=$'\033[1;31m'
NONE=$'\033[m'

If you do that, your first PS1 with the single quotes should work:

PS1='${RED}\h $(get_path) ${exitStatus}${NONE} '

However, then you will have a second problem.

Try running that, then press Up Arrow, then Home, and your cursor will not go back to the start of the line.

To fix that, change PS1 to include \[ and \] around the color escape sequences, e.g.

PS1='\[${RED}\]\h $(get_path) $?\[${NONE}\] '

You can't use get_exit_status properly here, since its output contains both printing (the exit code) and non-printing characters (the color codes), and there's no way to mark it correctly in the prompt. Putting \[...\] would mark it as non-printing in full, which is not correct. You'll have to change the function so that it only prints the proper color-code, and then surround it with \[...\] in the prompt.

3 deleted 1 characters in body
source | link

Gilles identified your main problem, but I wanted to try explaining it differently.

Bash is effectively calling echo on your PS1, not echo -e.

So it's like you're doing:

echo '\e[1;32m${HOME}\e[m''\e[1;32m\h\e[m'

If you try running that, you will see it doesn't work.

But bash gives you a way to write special characters that doesn't require using echo -e. It looks like $'\octal number'.

The special character in all the escape sequences is \e, which just means Escape. Escape's octal value is \033.

So we want it to expand to this instead:

echo $'\033[1;31m'"${HOMEHOSTNAME}"$'\033[m'

To do this, you can change your definition of GREEN, RED, and NONE, so their value is the actual escape sequence.

GREEN=$'\033[1;32m'
RED=$'\033[1;31m'
NONE=$'\033[m'

If you do that, your first PS1 with the single quotes should work:

PS1='${RED}\h $(get_path) ${exitStatus}${NONE} '

However, then you will have a second problem.

Try running that, then press Up Arrow, then Home, and your cursor will not go back to the start of the line.

To fix that, change PS1 to include \[ and \] around the color escape sequences, e.g.

PS1='\[${RED}\]\h $(get_path) \[${exitStatus}\]\[${NONE}\] '

And it should all be working.

(I'm not sure why putting \[ around ${exitStatus} works, because the exit status number shouldn't have those around it, but it seems to work for me.)

Gilles identified your main problem, but I wanted to try explaining it differently.

Bash is effectively calling echo on your PS1, not echo -e.

So it's like you're doing:

echo '\e[1;32m${HOME}\e[m'

If you try running that, you will see it doesn't work.

But bash gives you a way to write special characters that doesn't require using echo -e. It looks like $'\octal number'.

The special character in all the escape sequences is \e, which just means Escape. Escape's octal value is \033.

So we want it to expand to this instead:

echo $'\033[1;31m'"${HOME}"$'\033[m'

To do this, you can change your definition of GREEN, RED, and NONE, so their value is the actual escape sequence.

GREEN=$'\033[1;32m'
RED=$'\033[1;31m'
NONE=$'\033[m'

If you do that, your first PS1 with the single quotes should work:

PS1='${RED}\h $(get_path) ${exitStatus}${NONE} '

However, then you will have a second problem.

Try running that, then press Up Arrow, then Home, and your cursor will not go back to the start of the line.

To fix that, change PS1 to include \[ and \] around the color escape sequences, e.g.

PS1='\[${RED}\]\h $(get_path) \[${exitStatus}\]\[${NONE}\] '

And it should all be working.

(I'm not sure why putting \[ around ${exitStatus} works, because the exit status number shouldn't have those around it, but it seems to work for me.)

Gilles identified your main problem, but I wanted to try explaining it differently.

Bash is effectively calling echo on your PS1, not echo -e.

So it's like you're doing:

echo '\e[1;32m\h\e[m'

If you try running that, you will see it doesn't work.

But bash gives you a way to write special characters that doesn't require using echo -e. It looks like $'\octal number'.

The special character in all the escape sequences is \e, which just means Escape. Escape's octal value is \033.

So we want it to expand to this instead:

echo $'\033[1;31m'"${HOSTNAME}"$'\033[m'

To do this, you can change your definition of GREEN, RED, and NONE, so their value is the actual escape sequence.

GREEN=$'\033[1;32m'
RED=$'\033[1;31m'
NONE=$'\033[m'

If you do that, your first PS1 with the single quotes should work:

PS1='${RED}\h $(get_path) ${exitStatus}${NONE} '

However, then you will have a second problem.

Try running that, then press Up Arrow, then Home, and your cursor will not go back to the start of the line.

To fix that, change PS1 to include \[ and \] around the color escape sequences, e.g.

PS1='\[${RED}\]\h $(get_path) \[${exitStatus}\]\[${NONE}\] '

And it should all be working.

(I'm not sure why putting \[ around ${exitStatus} works, because the exit status number shouldn't have those around it, but it seems to work for me.)

2 added 588 characters in body; added 68 characters in body
source | link
1
source | link