The Issues:
Open command line prompt:
Enter the letter a multiple times:
Instead of wrapping to a new line the text entered wraps onto the same line:
Now, start pressing b. The second time a line wrap is required, it will wrap to a new line:
What causes this behavior?
Using a PS1 like this causes the behavior:
ps1Color="\033[1;35m"
export PS1='$(echo -en $ps1Color) Baz $'
Note the reason I want to use echo instead of the color directly is because I want to add the color conditionally based on the exit status of the previous command.
Using the color directly doesn't cause this behavior to occur.
My questions are:
- How can I print color codes for use in a PS1 using echo?
- If I want to make my PS1 a different color conditionally what is the best way to do this?
- Why am I seeing this behavior?
Update
To be clear, I really want to do this using echo because I want to change the color conditionally.
This is what I currently have right now:
function setPs1Colors_start () {
local previousExit=$?
local ps1Color="\033[1;35m"
local ps1FailBackground="\e[41m"
echo -en $ps1Color
if [[ previousExit -ne 0 ]]
then
echo -en $ps1FailBackground
fi
}
function setPs1Colors_end () {
local ps1DefaultColor="\033[0m"
echo -en $ps1DefaultColor
}
export PS1='$(setPs1Colors_start)[$(date +%b\-%d\ %k:%M)][$(versionControlInfo)\W]\$$(setPs1Colors_end) '
\e
inPS1
for a literal escape character:PS1='\e[1;35m Baz \$'
. – DopeGhoti May 26 '17 at 20:29$(tput bold)
inside the PS1 variable - use the ANSI escape sequences instead – Mudassir Feb 08 '20 at 13:24