Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1121627
  • 博文数量: 241
  • 博客积分: 4385
  • 博客等级: 上校
  • 技术积分: 2383
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-07 23:13
文章分类

全部博文(241)

文章存档

2013年(1)

2012年(8)

2011年(62)

2010年(109)

2009年(61)

分类: LINUX

2011-02-14 09:44:00

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 //当前工作目录的完整路径

  1. yuanlin@h ~> export PS1="\u@\h \w> "
  1. 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)

  1. yuanlin@yuanlin-desktop ~> export PS1="\u@\h [\$(date +%k:%M:%S)]> "
  1. 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来显示当前时间,按照 时:分:秒 的格式

  1. yuanlin@yuanlin-desktop [ 9:58:01]> export PS1="\u@\h [\t]> "
  1. 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 //最后一个命令的返回值

  1. yuanlin@yuanlin-desktop [10:01:49]> kernel_version=$(uname -r)
  1. yuanlin@yuanlin-desktop [10:04:53]> export PS1="\!|\h|$kernel_version|\$?> "
  1. 506|yuanlin-desktop|2.6.24-28-generic|0>
4. Change foreground color of the prompt更改提示符的前景色(即字符颜色)

  1. 506|yuanlin-desktop|2.6.24-28-generic|0> export PS1="\e[0;34m\u@\h \w> \e[m"
  1. yuanlin@yuanlin-desktop ~>
  1. yuanlin@yuanlin-desktop ~> export PS1="\e[1;34m\u@\h \w> \e[m"
  1. 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:颜色代码表
  1. Black 0;30
  2. Blue 0;34
  3. Green 0;32
  4. Cyan 0;36
  5. Red 0;31
  6. Purple 0;35
  7. Brown 0;33
  8. [Note: Replace 0 with 1 for dark color]
附2:使设置永久生效,修改.bash_profile或.bashrc文件
  1. STARTCOLOR='\e[0;34m';
  2. ENDCOLOR="\e[0m"
  3. export PS1="$STARTCOLOR\u@\h \w> $ENDCOLOR"
5. Change background color of the prompt更改提示符的背景色

  1. yuanlin@yuanlin-desktop ~> export PS1="\e[47m\u@\h \w> \e[m"
  2. [Note: This is for Light Gray background]
  3. [注: 设置背景色为浅灰色]
  1. yuanlin@yuanlin-desktop ~> export PS1="\e[0;34m\e[47m\u@\h \w> \e[m"
  2. [Note: This is for Light Blue foreground and Light Gray background]
  3. [注: 设置前景色为浅蓝色,背景色为浅灰色]
6. Create your own prompt using the available codes for PS1 variable

PS1变量表
  1. \a an ASCII bell character (07)
  2. \d the date in "Weekday Month Date" format (e.g., "Tue May 26")
  3. \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
  4. \e an ASCII escape character (033)
  5. \h the hostname up to the first part
  6. \H the hostname
  7. \j the number of jobs currently managed by the shell
  8. \l the basename of the shell's terminal device name
  9. \n newline
  10. \r carriage return
  11. \s the name of the shell, the basename of $0 (the portion following the final slash)
  12. \t the current time in 24-hour HH:MM:SS format
  13. \T the current time in 12-hour HH:MM:SS format
  14. \@ the current time in 12-hour am/pm format
  15. \A the current time in 24-hour HH:MM format
  16. \u the username of the current user
  17. \v the version of bash (e.g., 2.00)
  18. \V the release of bash, version + patch level (e.g., 2.00.0)
  19. \w the current working directory, with $HOME abbreviated with a tilde
  20. \W the basename of the current working directory, with $HOME abbreviated with a tilde
  21. \! the history number of this command
  22. \# the command number of this command
  23. \$ if the effective UID is 0, a #, otherwise a $
  24. \nnn the character corresponding to the octal number nnn
  25. \\ a backslash
  26. \[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
  27. \] end a sequence of non-printing character
6. References



Bash Cookbook, by Carl Albing, JP Vossen and Cameron Newham

阅读(1478) | 评论(0) | 转发(1) |
0

上一篇:grep

下一篇:umask

给主人留下些什么吧!~~