设置bash终端提示符(PS1)
来源:互联网 发布:ug编程培训 编辑:程序博客网 时间:2024/05/01 05:10
原文见:How to: Change/Setup bash custom prompt(PS1)PS1= Output \d \h $Sat May 05 Button[\d \t \u@\h:\W ] $ "[Sat May 05 22:18:30 xiayun@Button: ~ ] $
- 定制shell提示符的提示信息
- 给shell提示符添加颜色
定制shell提示符的提示信息
Bash是以转义字符\加上相应字符来定制提示信息,相应的含义如下表:- \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 '.'
- \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 characters
给shell提示符添加颜色
给shell提示符添加颜色有两种方法:
方法一:
给shell提示符添加颜色的语法:'\e[x;ym $PS1 \e[m'- \e[ Start color scheme
- x;y Color pair to use (x;y)
- $PS1 is your shell prompt
- \e[m Stop color theme
To set a red color prompt, add this into .bashrcPS='\e[0;31m[\u@\h \W]\$ \e[m '
方法二:
tput command
使用tput命令。例如显示红色的提示符:PS1=’\[$(tput setaf 1)\]\u@\h:\W $ \[$(tput sgr0)\]'
tput命令的使用有
- tput bold - Bold effect
- tput rev - Display inverse colors
- tput sgr0 - Reset everything
- tput setaf {CODE}- Set foreground color, see color {CODE} below
- tput setab {CODE}- Set background color, see color {CODE} below
Colors {code} code for tput command
Color {code}Color0Black1Red2Green3Yellow4Blue5Magenta6Cyan7WhiteExampe: Open /etc/bashrc (Redhat and friends) / or /etc/bash.bashrc (Debian/Ubuntu) or /etc/bash.bashrc.local (Suse and others) file and append following code:# vi /etc/bashrcor$ sudo gedit /etc/bashrcAppend the code as follows# If id command returns zero, you’ve root access.if [ $(id -u) -eq 0 ];then # you are root, set red colour prompt PS1="\\[$(tput setaf 1)\\]\\u@\\h:\\w #\\[$(tput sgr0)\\]"else # normal PS1="[\\u@\\h:\\w] $"fi
Save and source the file.
Read the man page of bash and tput command for more information
- 设置bash终端提示符(PS1)
- PS1~4,终端提示符设置
- PS1~PS4,终端提示符设置,终端配置
- Linux终端提示符PS1设置(颜色)
- Linux终端提示符颜色设置 – PS1
- 终端提示符设置(PS1变量配置)
- Linux中PS1-终端提示符设置
- linux-bash-提示符格式设置(PS1设置)
- 一份bash shell的PS1命令提示符设置脚本
- Linux系统下的终端命令提示符设置(PS1)
- ubuntu设置PS1提示符
- PS1设置Linux系统提示符
- linux命令提示符设置 PS1
- zsh设置命令行提示符PS1
- 自定义bash终端提示符
- Linux下修改终端提示符 PS1
- 修改Linux终端命令提示符颜色、PS1
- aix 下终端提示符变量设置 export PS1="[$LOGNAME@`uname -n`:\$PWD\/]#"
- 黑马程序员:我对责任链模式(Responsibility Chain)的理解
- 小tip: base64:URL背景图片与web页面性能优化
- Git学习教程(五):Git标签
- Git学习教程(六)Git日志
- Tomcat源码8
- 设置bash终端提示符(PS1)
- TCP&IP协议: TCP&IP协议族相关的RFC文档资料及源码
- apache 启动httpd.exe 报错 Could not reliably determine the server's fully qualified
- Git学习教程(七) Git差异比对
- HDU 4221 Greedy?(贪心)
- c# 提取HTML中所有图片URL
- MAMP for mac OS 帮助建立一个 server以及database
- 函数体中打印日志信息自动附加函数名字的一种实现办法
- Git 简明教程