4

I've been customizing my terminal prompt, and found that it's possible to change the "secondary prompt". However, I have no way to test the changes I make to this one so I was wondering if a simple script could be written to test the new $PS2?

  • 1
    Put " (double quote) on the terminal and press Enter. As bash does not see the matching quote to close the string, it will show PS2 to complete the multiline command. – Helio Apr 16 '15 at 15:04
5

From man bash:

   PS2    The value of this parameter is expanded as with PS1 and used  as
          the secondary prompt string.  The default is ``> ''.

The "secondary prompt string" is what is shown when you have multi-line input. For example:

$ echo 'foo
>bar'

The > shown above is PS2. If I were to change that, I would see it as soon as I tried any multi-line command:

$ PS2="%"
$ echo 'foo
%bar'

Ways to run multi-line commands include:

  • Open quotes (single or double) and hit enter without closing them.
  • Add a backslash to the end of a command and hit enter:

    $ echo foo \
    >
    
  • Use a HEREDOC:

    $ echo <<<END  ## hit enter
    

As for doing it with a script, that's really not needed but you can simply write a script that prints PS2:

#!/usr/bin/env bash
printf "PS2 : %s\n" "$PS2"
  • Your script doesn't show colored prompts and mine does ;-) echo -e or printf is needed to show escape characters correctly. – Helio Apr 16 '15 at 15:09

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.