How to Set a command prompt in Shell

Source: Internet
Author: User
Tags control characters
  • First, you must check that the shell you are using is the shell.

Echo $ Shell

 

  1. Bash settings

The bash prompt is determined by the variable PS1. You need to see your own PS1 settings and run the command

$ Set | grep PS1

You can see it. Similarly, if you want to add a color, you can also use this PS1 variable.

We are in the script ~ /. Set the end position of bashrc. This file is called when Bash is started.

We can directly initialize the PS1 variable by adding content in it.

The specific content is as follows:

PS1 = '\ [\ e [32m \] [\ [email protected] \ H \ W] # \ [\ e [M \]'

\ [\ E [32m \] is used to set the color. \ [\ E [M \] is used to disable the color settings. In this way, the prompt is set to the color we want.

 

Color Table
Foreground background color
---------------------------------------
30 40 black
31 41 colors
32 42 colors
33 43 colors
34 44 colors
35 45 Violet colors
36 46 green colors
37 47 white

Replace the 32 value of \ [\ e [32m \] with the number of the preceding foreground color to select a different color.

In addition, you can use special characters to define your prompt. The list below lists the most commonly used special characters.
Prompt special character code
Character meaning
\! Displays the history Number of the command.
\ # Display the command number of the current command.
\ $ Indicates the $ operator as the prompt. If the user is root, the # operator is displayed.
\ Display the backslash.
\ D displays the current date.
\ H displays the host name.
\ N print the new line.
\ NNN: displays the NNN octal value.
\ S displays the name of the currently running shell.
\ T displays the current time.
\ U displays the User Name of the current user.
\ W displays the name of the current working directory.
\ W displays the path of the current working directory.
These special characters can be combined into many useful prompt solutions (or can be combined into a very strange solution)

After setting, log on again or source ~ /. Bashrc or .~ /. Bashrc takes effect.

2. CSH settings

  

The command line prompt is pre-displayed in the command line. It usually displays some prompts, such as the current directory, login user name, and machine name. The method for setting a command line prompt in CSH/tcsh is similar to that in other shells, that is, setting the value of the variable prompt.

Set prompt = "% H: % [email protected] % m: % C>"

Some predefined variables are used in the CSH command line prompt. These predefined variables Enable dynamic display, such as the current path, command number, machine name, and logon user ID. With these variables, our CSH command line prompt can display the information we need. The predefined prompt parameters are as follows:

! Same as % H
% B start bold Mode
% B end bold Mode
% C the last part of the current working directory name
% C2, % C3, etc. The last two or three (and so on) components of the current working directory name
% C is similar to % C, but a long directory name is used.
% D the day of the week
% D today is the day of the month
% H history Number of the current command
% L current control terminal of Shell
% L clear all content from the end of the prompt to the next line
Host Name of % m Machine
% M machine's fully compliant Domain Name
% N User Name
% P indicates the 12-hour time displayed in exact format (including seconds)
% P is displayed in a precise format (including seconds) in a 24-hour format.
Standard output format starting from % s
% S end standard output format
% T display time in 12-hour format
% T display time in 24-hour format
% U start underline Mode
% U end underline Mode
% W name of the current month
% W the ordinal number of the current month
% Y indicates the year in 2-digit format
% Y year in 4-digit format
% Characters
%! Same as % H
% @ Is the same as % t
$ # Extended to> for general use. If the user is running as root, it is extended #
%/Full name of the current working directory
%-Current working directory in short format
^ Control characters matching C and C: for example, ^ g Represents Ctrl + G
\ A enables tcsh to beep when the prompt is displayed at any time
\ E Ended Escape Character 0x1b
\ F ended form feed
\ N starts a new line at this position
\ R carriage return
\ T Tab
\ V vertical Tab
\ NNN is a character specified by the octal number NNN.

 

 

How to Set a command prompt in Shell

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.