Bash Shell PS1: 10 Examples to Make Your Linux Prompt like Angelina Jolie[原文名称,标题不能太长⊙﹏⊙b汗]
说明:本文所有命令结果在本机运行验证,可以忽略yuanlin等机器相关的名称1. Display username, hostname and current working directory in the prompt在提示符中显示用户名,主机名以及当前工作目录\u – Username //用户名
\h – Hostname //主机名
\w – Full path of the current working directory //当前工作目录的完整路径
- yuanlin@h ~> export PS1="\u@\h \w> "
- yuanlin@yuanlin-desktop ~>
2. Display current time in the prompt在提示符中显示当前时间In the PS1 environment variable, you can directly execute any Linux command, by specifying in the format $(linux_command)
在环境变量PS1中,可以直接执行任何Linux命令,格式:$(linux_command)
- yuanlin@yuanlin-desktop ~> export PS1="\u@\h [\$(date +%k:%M:%S)]> "
- yuanlin@yuanlin-desktop [ 9:58:01]>
You can also use \t to display the current time in the hh:mm:ss format as shown below:
也可以使用\t来显示当前时间,按照 时:分:秒 的格式
- yuanlin@yuanlin-desktop [ 9:58:01]> export PS1="\u@\h [\t]> "
- yuanlin@yuanlin-desktop [10:01:49]>
3. Display output of any Linux command in the prompt
在提示符中显示任何Linux命令的结果\!: The history number of the command //命令的历史记录数
\h: hostname //主机名
$kernel_version: The output of the uname -r command from $kernel_version variable //内核版本
\$?: Status of the last command //最后一个命令的返回值
- yuanlin@yuanlin-desktop [10:01:49]> kernel_version=$(uname -r)
- yuanlin@yuanlin-desktop [10:04:53]> export PS1="\!|\h|$kernel_version|\$?> "
- 506|yuanlin-desktop|2.6.24-28-generic|0>
4. Change foreground color of the prompt更改提示符的前景色(即字符颜色)- 506|yuanlin-desktop|2.6.24-28-generic|0> export PS1="\e[0;34m\u@\h \w> \e[m"
- yuanlin@yuanlin-desktop ~>
- yuanlin@yuanlin-desktop ~> export PS1="\e[1;34m\u@\h \w> \e[m"
- yuanlin@yuanlin-desktop ~>
\e[ - Indicates the beginning of color prompt //颜色设置开始标示
x;ym - Indicates color code. Use the color code values mentioned below //颜色标示
\e[m - indicates the end of color prompt //颜色设置结束标示
附1:颜色代码表
- Black 0;30
-
Blue 0;34
-
Green 0;32
-
Cyan 0;36
-
Red 0;31
-
Purple 0;35
-
Brown 0;33
-
[Note: Replace 0 with 1 for dark color]
附2:使设置永久生效,修改.bash_profile或.bashrc文件
- STARTCOLOR='\e[0;34m';
-
ENDCOLOR="\e[0m"
-
export PS1="$STARTCOLOR\u@\h \w> $ENDCOLOR"
5. Change background color of the prompt更改提示符的背景色- yuanlin@yuanlin-desktop ~> export PS1="\e[47m\u@\h \w> \e[m"
- [Note: This is for Light Gray background]
- [注: 设置背景色为浅灰色]
- yuanlin@yuanlin-desktop ~> export PS1="\e[0;34m\e[47m\u@\h \w> \e[m"
-
[Note: This is for Light Blue foreground and Light Gray background]
-
[注: 设置前景色为浅蓝色,背景色为浅灰色]
6. Create your own prompt using the available codes for PS1 variablePS1变量表
- \a an ASCII bell character (07)
-
\d the date in "Weekday Month Date" format (e.g., "Tue May 26")
-
\D{format} - the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
-
\e an ASCII escape character (033)
-
\h the hostname up to the first part
-
\H the hostname
-
\j the number of jobs currently managed by the shell
-
\l the basename of the shell's terminal device name
-
\n newline
-
\r carriage return
-
\s the name of the shell, the basename of $0 (the portion following the final slash)
-
\t the current time in 24-hour HH:MM:SS format
-
\T the current time in 12-hour HH:MM:SS format
-
\@ the current time in 12-hour am/pm format
-
\A the current time in 24-hour HH:MM format
-
\u the username of the current user
-
\v the version of bash (e.g., 2.00)
-
\V the release of bash, version + patch level (e.g., 2.00.0)
-
\w the current working directory, with $HOME abbreviated with a tilde
-
\W the basename of the current working directory, with $HOME abbreviated with a tilde
-
\! the history number of this command
-
\# the command number of this command
-
\$ if the effective UID is 0, a #, otherwise a $
-
\nnn the character corresponding to the octal number nnn
-
\\ a backslash
-
\[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
-
\] end a sequence of non-printing character
6. References
Bash Cookbook, by Carl Albing, JP Vossen and Cameron Newham
阅读(1123) | 评论(0) | 转发(0) |