According to man bash
, in the PS1
environment variable (defining the shell prompt):
\[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
\] end a sequence of non-printing characters
And according to various examples I've seen, I'm supposed to surround non-printing characters sequences with \[
and \]
- but it doesn't seem to make any difference whether I include them!
For example:
export PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n[\t]\$ '
appears to work no differently from:
export PS1='\e]0;\w\a\n\e[32m\u@\h \e[33m\w\e[0m\n[\t]$ '
(i.e. the same but with \[
and \]
removed).
So what's the advantage of enclosing sequences of non-printing characters in this way? Is it for when outputting to a dumb terminal (which I never need to do), so that the non-printing characters can be ignored? (Presumably PS1
is ignored in non-interactive sessions, so I guess it can't be for when scripting.)