1

I know that it's possible to customize the prompt (as detailed in the 6.9 Controlling the Prompt section from Bash manual) and I have done it for a while, but I recently noticed some strange behavior.

Consider the two following scenarios:

Without escape sequences

PS1='\$ '
PS2='> '
PS3='#? '
PS4='+ '

Without escape sequences

With escape sequences

PS1='\[\e[1;34m\]\$\[\e[0m\] '
PS2='\[\e[1;34m\]>\[\e[0m\] '
PS3='\[\e[1;34m\]#?\[\e[0m\] '
PS4='\[\e[1;34m\]+\[\e[0m\] '

With escape sequences

So, the problems are:

  • PS3 is printed as is, without interpreting the escape sequences.
  • PS4 is not even printed.

I'm pretty sure that they used to work before, but as I don't use them very often I have no clue since when they are misbehaving.


Technical details

  • OS: Ubuntu 16.04.4
  • Shell: Bash 4.3.48(1)-release
  • Terminal emulator: GNOME Terminal 3.18.3 (it also happens in virtual terminals, though)
  • There hasn't been any Bash update since system was installed (2017-06-09), as far as I know.
1

From man bash:

PS1    The value of this parameter is expanded (see PROMPTING below) and used as the primary prompt string.  The default value is ``\s-\v\$ ''.
PS2    The value of this parameter is expanded as with PS1 and used as the secondary prompt string.  The default is ``> ''.
PS3    The value of this parameter is used as the prompt for the select command (see SHELL GRAMMAR above).
PS4    The value of this parameter is expanded as with PS1 and the value is printed before each command bash displays  during  an  execution  trace.   The

So for whatever reason the non-expansion of PS3 is documented behavior.

As for PS4 you need to export the variable to make it available in a new invocation of bash. And you need to set the trace option explicitly, -v doesn't turn it on:

pse@Mithos:~/.tmp$ export PS4='uuuu: '
pse@Mithos:~/.tmp$ bash -c "set -x; echo foo"
uuuu: echo foo
foo
  • Well, that was a pretty embarrassing error from my part. Thanks a lot for answer. – nxnev Apr 23 '18 at 16:15
1

From the bash manual:

   PS1    The  value of this parameter is expanded (see PROMPTING below) and used as the primary prompt string.  The default
          value is ``\s-\v\$ ''.
   PS2    The value of this parameter is expanded as with PS1 and used as the secondary prompt string.  The default  is  ``>
          ''.
   PS3    The value of this parameter is used as the prompt for the select command (see SHELL GRAMMAR above).
   PS4    The  value  of  this  parameter is expanded as with PS1 and the value is printed before each command bash displays
          during an execution trace.  The first character of PS4 is replicated multiple times,  as  necessary,  to  indicate
          multiple levels of indirection.  The default is ``+ ''.

The definition of PS3 does not state that it is expanded in the same manner as the other prompt strings. The behavior you see is consistent with the documentation.

  • I accepted patrix's answer as it also included the explanation about PS4, but thanks for answer. – nxnev Apr 23 '18 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.