Using Secondary Prompts: $PS2, $PS3, $PS4

Problem

You’d like to understand what the $PS2, PS3, and PS4 prompts do.

Solution

$PS2 is called the secondary prompt string and is used when you are interactively entering a command that you have not completed yet. It is usually set to “> " but you can redefine it. For example:

[jp@freebsd jobs:0]
/home/jp$ export PS2='Secondary: '

[jp@freebsd jobs:0]
/home/jp$ for i in $(ls)
Secondary: do
Secondary: echo $i
Secondary: done
colors
deepdir
trunc_PWD

$PS3 is the select prompt, and is used by the select statement to prompt the user for a value. It defaults to #?, which isn’t very intuitive. You should change it before using the select command; for example:

[jp@freebsd jobs:0]
/home/jp$ select i in $(ls)
Secondary: do
Secondary: echo $i
Secondary: done
1) colors
2) deepdir
3) trunc_PWD
#? 1
colors
#? ^C

[jp@freebsd jobs:0]
/home/jp$ export PS3='Choose a directory to echo: '

[jp@freebsd jobs:0]
/home/jp$ select i in $(ls); do echo $i; done
1) colors
2) deepdir
3) trunc_PWD
Choose a directory to echo: 2
deepdir
Choose a directory to echo: ^C

$PS4 is displayed during trace output. Its first character is shown as many times as necessary to denote the nesting depth. The default is “+ “. For example:

[jp@freebsd jobs:0] /home/jp$ cat demo #!/usr/bin/env bash set -o xtrace alice=girl echo "$alice" ls -l $(type -path vi) echo line 10 ech0 line 11 echo line 12 [jp@freebsd jobs:0] /home/jp$ ./demo + alice=girl + echo girl girl ++ type -path vi + ls -l /usr/bin/vi ...

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.