1.提示符中显示用户名,主机名和当前工作目录

这个例子中PS1将在提示符中显示以下三种信息\u – 用户名

\h – 主机名

\w – 当前工作目录的绝对路径-bash-3.2$ export PS1="\u@\h \w> "

ramesh@dev-db ~> cd /etc/mail

ramesh@dev-db /etc/mail>

2. 提示符中显示当前时间

在PS1环境变量中,你可以使用$(linux_command)这种格式直接执行任何linux指令,在下面的例子中,通过执行指令$(date)在提示符中显示当前时间ramesh@dev-db ~> export PS1="\u@\h [\$(date +%k:%M:%S)]> "

ramesh@dev-db [11:09:56]>

你也可以使用\t以hh:mm:ss格式显示当前的时间ramesh@dev-db ~> export PS1="\u@\h [\t]> "

ramesh@dev-db [12:42:55]>

你也可以像下面的示例一样使用\@ 以12小时am/pm格式显示当前时间ramesh@dev-db ~> export PS1="[\@] \u@\h> "

[04:12 PM] ramesh@dev-db>

3. 在提示符中显示任一linux指令输出

你可以在提示符中显示任何linux指令的输出.下面的例子在提示符中显示三项用|(pipe)分割的信息\!: 显示历史指令记录数

\h: 主机名

$kernel_version:  uname -r 指令的输出

\$?: 上一条指令的状态ramesh@dev-db ~> kernel_version=$(uname -r)

ramesh@dev-db ~> export PS1="\!|\h|$kernel_version|\$?> "

473|dev-db|2.6.25-14.fc9.i686|0>

4. 更改提示符的前景色

Display prompt in blue color, along with username, host and current directory information

使用蓝色显示提示符,包括用户名,主机名和当前工作目录信息

$ export PS1="\e[0;34m\u@\h \w> \e[m" [Note: This is for light blue prompt]  $ export PS1="\e[1;34m\u@\h \w> \e[m" [Note: This is for dark blue prompt]\e[ - 提示符颜色的开始处

x;ym - 颜色值., 使用下面提到的数值

\e[m - 提示符颜色的结束处

Color Code Table: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]

将下面指令添加到.bash_profile或者.bashrc中,使其永久生效STARTCOLOR='\e[0;34m';

ENDCOLOR="\e[0m"

export PS1="$STARTCOLOR\u@\h \w> $ENDCOLOR"

5. 更改提示符背景色

Change the background color by specifying \e[{code}m in the PS1 prompt as shown below.

想下面的例子一样在PS1中指定\e[{code}m 的值以更改背景色$ export PS1="\e[47m\u@\h \w> \e[m"  [Note: This is for Light Gray background]

组合背景色和前景色export PS1="\e[0;34m\e[47m\u@\h \w> \e[m"  [Note: This is for Light Blue foreground and Light Gray background]

将下面的代码添加到.bash_profile或者.bashrc中使背景色和前景色永久生效.STARTFGCOLOR='\e[0;34m'; STARTBGCOLOR="\e[47m" ENDCOLOR="\e[0m" export PS1="$STARTFGCOLOR$STARTBGCOLOR\u@\h \w> $ENDCOLOR"

从下面的背景色中选择最符合你口味儿的颜色.\e[40m

\e[41m

\e[42m

\e[43m

\e[44m

\e[45m

\e[46m

\e[47m

6. 在提示符中显示混合色

你也可以在同一提示符中显示混合色,将下面的函数添加到.bash_profile中function prompt {   local BLUE="\[\033[0;34m\]"   local DARK_BLUE="\[\033[1;34m\]"   local RED="\[\033[0;31m\]"   local DARK_RED="\[\033[1;31m\]"   local NO_COLOR="\[\033[0m\]"   case $TERM in     xterm*|rxvt*)       TITLEBAR='\[\033]0;\u@\h:\w\007\]'       ;;     *)       TITLEBAR=""       ;;   esac   PS1="\u@\h [\t]> "   PS1="${TITLEBAR}\   $BLUE\u@\h $RED[\t]>$NO_COLOR "   PS2='continue-> '   PS4='$0.$LINENO+ ' }

你可以重新登陆使修改生效,或者像下面一样source .bash_profile$. ./.bash_profile $ prompt ramesh@dev-db [13:02:13]>

7. 用tput更改提示符颜色

你也可以像下面一样在PS1中使用tput更改颜色$ export PS1="\[$(tput bold)$(tput setb 4)$(tput setaf 7)\]\u@\h:\w $ \[$(tput sgr0)\]"

tput Color Capabilities:tput setab [1-7] - Set a background color using ANSI escape

tput setb [1-7] - Set a background color

tput setaf [1-7] - Set a foreground color using ANSI escape

tput setf [1-7] - Set a foreground color

tput Text Mode Capabilities:tput bold - Set bold mode

tput dim - turn on half-bright mode

tput smul - begin underline mode

tput rmul - exit underline mode

tput rev - Turn on reverse mode

tput smso - Enter standout mode (bold on rxvt)

tput rmso - Exit standout mode

tput sgr0 - Turn off all attributes

Color Code for tput:0 - Black

1 - Red

2 - Green

3 - Yellow

4 - Blue

5 - Magenta

6 - Cyan

7 - White

8. 使用下面的PS1变量可用代码制作个性化的提示符

使用下面的代码制作一个符合你自己口味儿和所需要功能的个性化提示符.\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

9. 在PS1变量中使用bash shell函数

你也可以在PS1中像下面一样调用一个bash shell函数ramesh@dev-db ~> function httpdcount { >  ps aux | grep httpd | grep -v grep | wc -l > }  ramesh@dev-db ~> export PS1="\u@\h [`httpdcount`]> " ramesh@dev-db [12]> [Note: This displays the total number of running httpd processes]

你可以将下面的代码添加到.bash_profile或者.bashrc中使其永久生效function httpdcount {   ps aux | grep httpd | grep -v grep | wc -l } export PS1='\u@\h [`httpdcount`]> '

10. 在PS1变量中使用shell脚本

你也可以在PS1中调用一个shell脚本,在下面的例子中,~/bin/totalfilesize.sh这个脚本被PS1变量调用,以显示当前目录总的文件数,ramesh@dev-db ~> cat ~/bin/totalfilesize.sh

for filesize in $(ls -l . | grep "^-" | awk '{print $5}')

do

let totalsize=$totalsize+$filesize

done

echo -n "$totalsize"

ramesh@dev-db ~> export PATH=$PATH:~/bin

ramesh@dev-db ~> export PS1="\u@\h [\$(totalfilesize.sh) bytes]> "

ramesh@dev-db [534 bytes]> cd /etc/mail

ramesh@dev-db [167997 bytes]>11. IP地址示例

my_ip=$(/sbin/ip addr show dev eth1 | perl -ne '/inet ([\d.]+)/ && print $1')

export PROMPT_COMMAND='echo -ne "\033]0;$my_ip\007"'

export PS1="\[\e[1;36m\][\H]\[\e[31;1m\]\u\[\e[0m\]@\[\e[32;1m\]$my_ip\[\e[0m\]:\[\e[35;1m\]\w\[\e[0m\]\\$ "

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

linux ps1 主机名 ip,Bash Shell PS1: 自定义你的linux提示符十例相关推荐

  1. linux ps1 主机名 ip,Linux主机名颜色设置,ps1设置,然linux有颜色

    RedHat的字体和背景颜色的改变方法: 命令: PS1="[\e[32;1m\u@\h \W]\\$" 或 export PS1="[\e[32;1m\u@\h \W] ...

  2. Linux 修改主机名的两种方法

    Linux 修改主机名的两种方法 一.使用Linux系统命令修改主机名 通过man获取帮助 hostnamectl 用法 修改 重启生效 二.通过修改配置文件修改主机名 编辑配置文件 修改 重启生效 ...

  3. Linux修改主机名的两种方法

    Linux修改主机名的两种方法 文章目录 先展示一下效果 一.通过hostname命令直接更改主机名(不是永久) 1.显示当前的主机名 2.更改主机名 二.通过修改配置文件(永久改) 1.hostna ...

  4. Linux修改主机名(静态主机名、临时主机名)

    背景 通常情况下Linux在安装时会设置主机名.root密码等相关参数,但安装后的使用过程中或许你需要修改主机名,本文讲述如何修改主机名,包括临时修改和永久修改. 查看主机名 原生态的Linux一般自 ...

  5. Oracle中tnsping 主机名/Ip 显示TNS-12541: TNS:no listener

    今天是个阴天,老天想下不下的,昏昏沉沉的总想睡觉-- 额--废话不多说了. 今天中午想做一个catalog库,我就在虚拟机上装了Oracle11g,本想不同于target数据库, 所以当时配置监听时就 ...

  6. Linux hostname主机名配置文件与文件 /etc/hosts解说

    Linux hostname经过长时间的发展,这里我发表一下个人理解,下面就这就来讲术Linux hostname.今天又开始写网络文档了,先写一篇小一点的练练手,本来计划了一篇比较大的网络基础文档, ...

  7. 总结Linux修改主机名的四种方式

    总结Linux修改主机名的四种方式 看网上很多文章,有些比较简洁,但是有些很繁琐,不多说,参考各路大神的文章,以下是本人对这几种方式进行简要介绍,如有不足之处,还望各位大佬指点迷津. 方式一(个人推荐 ...

  8. Linux修改主机名--立即生效的方法

    Linux修改主机名–立即生效的方法 在使用Linux操作系统过程中,如果我们想更改主机名时,可以用以下命令进行操作 查看主机名 [root@ecs-5332 ~]# hostname ecs-533 ...

  9. Linux配置主机名

    Linux配置主机名 设置本机主机名 添加主机名称与IP的映射关系 设置本机主机名 编辑/etc/sysconfig/network vi /etc/sysconfig/network 添加配置 HO ...

  10. Linux修改主机名永久生效

    Linux修改主机名 Linux修改主机名,永久生效. linux查看主机名: 查看主机命令: [root@linux_epm2 ~]# hostname localhost.localdomain ...

最新文章

  1. 微生物组—宏基因组分析专题培训7月开课啦!北京
  2. 2-01基本顺序表与元素外置顺序表recv
  3. Bug之ajax不执行
  4. 工作一年后,我有些感悟(写于2017年)
  5. iOS 6 的5个新特性创建杀手级应用
  6. centos常用命令_二、Docker镜像是什么?Docker常用命令
  7. java如何解决高并发症,JAVA线上故障紧急处理详细过程!
  8. 浏览器 UC 自动添加关键字 去掉关键字
  9. 4个空格和一个tab有什么区别_为什么有时候会放屁连连?这4个原因,一个都别放过...
  10. Android Service用法知识点的讲解
  11. 【狂神说Redis】1NoSQL概述 1-1为什么使用NoSQL
  12. python 做界面时如何使图片保持透明背景_Python利用matplotlib生成图片背景及图例透明的效果...
  13. 数学教育与计算机教育ppt,计算机基础教育课件.ppt
  14. java读取tif文件_java读取TIF,TIFF文件方法
  15. Andriod --- JetPack :LiveData setValue 和 postValue 的区别
  16. 中标麒麟yum源地址
  17. bugku-web-滑稽
  18. 威锋十年:叫一声「果粉」,你还敢答应吗?
  19. CAD设计大神这样玩CAD!而你真的会用外部参照嘛?
  20. 最新计算机java毕业设计选题题目推荐

热门文章

  1. 【M1兼容】草图大师mac 英文版 SketchUp 2021 Mac
  2. 中国农业大学计算机研究生专业课,2020年中国农业大学计算机科学与技术考研经验分享...
  3. 网工必须要了解BGP外部网关路由选择协议
  4. 如何快速成为CSDN的博客专家,以及「博客专家」申请及审核执行标准
  5. Albumentation使用指南
  6. 超市网店营销与接口测试
  7. 安卓开发(简单打开前置摄像头并显示)
  8. MAC系统如何显示隐藏文件解决方法
  9. “企业级零代码黑客马拉松大赛”决赛名单公布
  10. 行列式与矩阵树定理入门