Home ^[ escape in shell prompt

# ^[ escape in shell prompt

gwelof
1#
gwelof Published in 2018-02-13 18:59:50Z

### Problem

I am writing an application in rust (for fun) that allows me to use DSL to pretty-print git repository stats into my bash shell prompt.

However, I am having an issue with the library I use to format text in the terminal. It uses an escape I haven't seen before, ^[, in the output. It seems to affect the line wrapping such that the text wraps onto the same line before the end of the terminal.

You can see here a demonstration in gnome shell and in hyper.

### Replication

Create a file shell.txt with the following contents:

^[[1;32m\u^[[0m^[[32m@\h^[[0m:^[[1;34m\w^[[0m


Set your prompt command to be the following:

__set_prompt() {
PS1="\$(cat shell.txt)"
}
PROMPT_COMMAND=__set_prompt


### Question

What is the ^[ escape?

Is there a simple fix to get my shell to recognize the line width-properly?

bash --version 4.4.12(1)-release

chepner
2#
 It's a representation of a literal escape character (ASCII 0x1b). A simpler way to write shell.txt would be \e[1;32m\u\e[0m\e[32m@\h\e[0m:\e[1;34m\w\e[0m  To make sure the shell can accurately compute the size of your prompt, you need to wrap the non-printing characters in $...$. $\e[1;32m$\u$\e[0m\e[32m$@\h$\e[0m$:$\e[1;34m$\w$\e[0m$