InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Using Secondary Prompts: $PS2, $PS3, $PS4 in bash
Bash

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

InfinityCoder April 5, 2017

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

$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:

1
2
3
4
5
6
7
8
9
10
11
[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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[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
-r-xr-xr-x 6 root wheel 285108 May 8 2005 /usr/bin/vi
+ echo line 10
line 10
+ ech0 line 11
./demo: line 11: ech0: command not found
+ echo line 12
line 12
 
[jp@freebsd jobs:0]
/home/jp$ export PS4='+xtrace $LINENO: '
[jp@freebsd jobs:0]
/home/jp$ ./demo
+xtrace 5: alice=girl
+xtrace 6: echo girl
girl
++xtrace 8: type -path vi
+xtrace 8: ls -l /usr/bin/vi
-r-xr-xr-x 6 root wheel 285108 May 8 2005 /usr/bin/vi
+xtrace 10: echo line 10
line 10
+xtrace 11: ech0 line 11
./demo: line 11: ech0: command not found
+xtrace 12: echo line 12
line 12

The $PS4 prompt uses the $LINENO variable, which when used in a function under versions of bash prior to 2.0 returns the number of simple commands executed, rather than the actual line number in the function.

Also note the single quotes, which defer expansion of the variable until display time.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Summing a List of Numbers in bash
You need to sum a list of numbers, including numbers …

Summing a List of Numbers in bash

Showing Where You Are in bash
You are not sure what directory you are in, and …

Showing Where You Are in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap