3

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.

2 Answers 2

4

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
1
  • Well, that was a pretty embarrassing error from my part. Thanks a lot for answer. Commented Apr 23, 2018 at 16:15
2

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.

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.