Linux under PS1 PS2 PS3 PS4 Prompt detailed

Source: Internet
Author: User

Many people in the use of Linux, the prompt is not too much attention, can use a good prompt can be more intuitive to see, for their own judgment

Linux has PS1 PS2 PS3 PS4 Four class prompt, these four variables are environment variables, as to what environment variables in the system, you can use the command "env" to view

PS1 Command Prompt

PS1 has those configuration, or PS1, that can be configured with a command prompt:

\d: represents the date, formatted as weekday month date, for example: "Mon-1"

\h: The full host name. For example: My machine name is: Fc4.linux, then this name is Fc4.linux

\h: only the first name of the host is taken, as in the example above, then Fc4,.linux is omitted

\ t : Display time in 24-hour format such as: HH:MM:SS

\ t : Display time in 12-hour format

\a: display time in 24-hour format: hh:mm

\u: Current user's account name

version Information for \v:bash

\w: The full working directory name. Home directory will be replaced by ~

\w: use basename to get the working directory name, so only the last directory is listed

\#: The first few commands issued

\$: prompt character, if root, Prompt is: #, normal user is: $

the format for setting character sequence colors in PS1 is: \[\e[f; Bm\] where "F" is the font color, number 30~37; "B" is the background color, number 40~47. However, can set the color, he will certainly be able to cancel the settings, note: Here the cancellation settings, and not set is two things! Cancel setting: \[\e[m\]

Color table

foreground background color

- black

to red

+ Green

- yellow

the Blue

* purple red

$ cyan Blue

Panax Notoginseng white

No one is silly to set the white bar .....

Code meaning

0 OFF

1 highlight display

4 underline c23>

7 anti- white display

8 not visible

[\[\e[32m\]#\##\[\e[31m\]\[email protected]\[\e[36m\]\h \w]\$\[\e[m\]

This is my configuration.

now look at \[\e[32m\], note: The front square brackets are not used here. That's not the name. To set to Green , take a look at my command prompt:[#4 #[email protected] The cp ~]$ has a green bar.

Then #\# #就是显示现在运行的是第几条命令了, see. I'm here in fourth one.

down again a set of color [\e[31m\], is set to red bar, this does not have red. [#4 #[email protected]CP ~]$. Hehe

Down is \[email protected] what does that mean? I look at the table, and then not a color of it (shown here is not very good),

\h What do you mean, \w? \h is saying: Take the first name of the host only, \w is said: Show the complete path, but do not know why the home he shows ~ instead of the absolute path.

in down not a ] . There is no one at the beginning . These two are exactly paired together.

and then the most of this \[\e[m\] use to turn off the color setting. If you do not have this, then your command prompt, including what you output through the command prompt, is the same as the last color setting (except for some special-purpose files ).

". bashrc" in the root directory of the user notice there's a "." in front. the representation is hidden.

The last line in the inside adds:

Ps1= ' [\[\e[32m\]#\##\[\e[31m\]\[email protected]\[\e[36m\]\h \w]\$\[\e[m\] '

and then save. Then source . BASHRC

ps2--talk about the prompt again

A very long command can be displayed by adding "\" to its branch at the end. The default prompt for multi-line commands is ">". We can modify the PS2 to change the prompt to "continue->".

[email protected] ~> myisamchk--silent--force--fast--update-state \ 
>--key_buffer_size=512M- -sort_buffer_size=512m \ 
>--read_buffer_size=4m--write_buffer_size=4m \ 
>/var/lib/mysql/ bugs/*. MYI
[Note: This uses > as the default prompt]
[email protected] ~> export ps2= "continue->"  
[email  Protected] ~> myisamchk--silent--force--fast--update-state \ 
continue->--key_buffer_size=512m-- sort_buffer_size=512m \ 
continue->--read_buffer_size=4m--write_buffer_size=4m \ 
continue- >/var/lib/mysql/bugs/*. MYI
[Note: Similar to above, use "continue->" as a prompt]

When I use "\" to make long command branches appear, I find it very easy to read. Of course, I've seen some people who don't like the branch display command.

3. Prompt when using select in Ps3--shell script

You can use the environment variable PS3 to customize the shell script's select hint as demonstrated below:

Script output that does not use PS3:

1

2

3

4

5

6

7

8

9

Ten

One

[email protected] ~> Cat ps3.sh

Select I in Mon Tue Wed exit

Does

    case $i in

         mon) echo "Monday";;

        tue) echo "Tuesday";;

        wed) echo "Wednesday";;

        exit) exit;;

    esac

Done

 

[email protected] ~>./ps3.sh

1) Mon

2) Tue

3) Wed

4) exit

#? 1

Monday

#? 4

[Note: The default prompt is #?]

Script output using PS3:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

[email protected] ~> cat ps3.sh

ps3= "Select a Day (1-4):"

Select I in Mon Tue Wed exit

Do

    case $i in

        mon) echo "Monday";;

        tue) echo "Tuesday";;

        wed) echo "Wednesday";;

        exit) exit;;

    esac

Done

 

[email protected] ~>./ps3.sh

1) Mon

2) Tue

3) Wed

4) Exit

Select a day (1-4): 1

Monday

Select a day (1-4): 4

[Note: After setting the PS3 variable, the command prompt has become "Select a Day (1-4):"]

4. ps4--ps4-"Set-x" to modify the prefix of the trace output

If you are in a script in debug mode as follows, the PS4 environment variable can be used to customize the prompt message:

The shell script output when PS4 is not set:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

[email protected] ~> cat ps4.sh

Set–x

echo "PS4 Demo Script"

Ls-l/etc/| Wc–l Du-sh ~

[Email protected] ~>./ps4.sh

+ + Echo ' PS4 demo script '

PS4 Demo Script

+ + ls-l/etc/

+ + Wc–l

243

+ + Du-sh/home/ramesh

48k/home/ramesh

[Note: When using sex-x trace output, the prompt is + +]

Set the script output after PS4:

PS4 defines the following two variables in the ps.sh

o $ show Current pin name
o $LINENO the current line number displayed

1

2

3

4

5

6

7

8

9

13

[email  Protected] ~> cat ps4.sh

Export ps4= ' $ $LINENO + '

set-x

Echo ' PS4 demo script '

Ls-l/etc/| Wc-l

Du-sh ~

 

[email protected] ~>./ps4.sh

. /ps4.sh.3+ Echo ' PS4 demo script '

PS4 demo script

... /ps4.sh.4+ ls-l/etc/

: /ps4.sh.4+ wc-l

243

: /ps4.sh.5+ Du-sh/home/ramesh

48k/home/ramesh

[Note: Make ' {script-name} ' after using PS4. {line-number}+ "becomes set–x command prompt]



This article is from the "Lonely No Color" blog, please be sure to keep this source http://tenderness.blog.51cto.com/8855468/1954367

Linux under PS1 PS2 PS3 PS4 Prompt detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.