3

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.)

IpsRich
  • 797
  • 10
  • 23
  • Prevents wrapping problems i think –  Sep 01 '14 at 14:44
  • 3
    This question might be relevant: http://superuser.com/questions/301353/escape-non-printing-characters-in-a-function-for-a-bash-prompt – Christian Sep 01 '14 at 14:46
  • 2
    It's to avoid the problems described in these questions: [1](http://stackoverflow.com/questions/17432993/adding-ansi-color-escape-sequences-to-a-bash-prompt-results-in-bad-cursor-positi), [2](http://stackoverflow.com/questions/19092488/custom-bash-prompt-is-overwriting-itself), [3](http://superuser.com/questions/513458/custom-terminal-prompt-overwrites-on-itself-and-generally-behaves-improperly-on), [4](http://superuser.com/questions/525633/why-are-some-often-long-commands-garbled-in-bash-zsh) (and many others). – Gordon Davisson Sep 01 '14 at 15:39

0 Answers0