Q. How do I change the color of my shell prompt under Linux ? A. You can change the color of your shell prompt to impress your friend or to make your own life quite easy while working at command prompt. In the Linux default shell is BASH. Your current prompt setting is stored in PS1 shell variable. There are other variables too, like PS2, PS3 and PS4. Bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters. Task: Display current BASH prompt (PS1)Use echo command to display current BASH prompt: [\u@h \W]\$ By default the command prompt is set to: [\u@\h \W]\$. Backslash-escaped special characters are decoded as follows:
Task: Modify current BASH promptUse export command to setup a new shell prompt: Where,
Task: Add colors to the promptTo add colors to the shell prompt use the following export command syntax: Where,
To set a red color prompt, type the command: List of Color code
Replace digit 0 with 1 to get light color version. Task: How to make the prompt setting permanentYour new shell prompt setting is temporary i.e. when you logout setting will be lost. To have it set everytime you login to your workstation add above export command to your .bash_profile file or .bashrc file. Save and close the file. tput commandYou can also use tput command. For example display RED prompt use tput as follows: handy tput commands
Colors {code} code for tput command
Read the man page of bash and tput command for more information.
ANSI控制码的说明
例如: echo -ne "\33[32m" 可以将字符的显示颜色改为绿色 echo -ne "\33[3;1H" 可以将光标移到第3行第1列处 具体的摘抄一些如下: \33[0m 关闭所有属性 \33[1m 设置高亮度 \33[4m 下划线 \33[5m 闪烁 \33[7m 反显 \33[8m 消隐 \33[30m -- \33[37m 设置前景色 \33[40m -- \33[47m 设置背景色 \33[nA 光标上移n行 \33[nB 光标下移n行 \33[nC 光标右移n行 \33[nD 光标左移n行 \33[y;xH设置光标位置 \33[2J 清屏 \33[K 清除从光标到行尾的内容 \33[s 保存光标位置 \33[u 恢复光标位置 \33[?25l 隐藏光标 \33[?25h 显示光标 所有的转移字符表:
这样,您已经知道了 bash 中用反斜杠转义的全部专用序列。请稍微演练一下这些序列,以对它们的工作方式获得一些感性认识。在您做了一些测试之后,下面开始添加颜色。 ======================================== for attr in 0 1 4 5 7 ; do |
shell 颜色控制
shell 颜色控制
2008-06-27 12:53
猜你喜欢
转载自jarit.iteye.com/blog/1233237
今日推荐
周排行