Ubuntu 修改 PS1 美化命令提示符

介绍

从 centos 换到 Ubuntu 后看着命令提示符很不舒服,想要改成 centos 的格式。

查看相关帮助信息

root@Ubuntu:~# man bash

我们可以看到以下说明,表示配置文件在 ~/.bashrc

       Bash attempts to determine when it is being run with its standard  input  connected  to  a network  connection,  as  when  executed  by the remote shell daemon, usually rshd, or the secure shell daemon sshd.  If bash determines it is being run in this  fashion,  it  reads and executes commands from ~/.bashrc and ~/.bashrc, if these files exist and are readable.
       It will not do this if invoked as sh.  The --norc option  may  be  used  to  inhibit  this behavior,  and  the --rcfile option may be used to force another file to be read, but neither rshd nor sshd generally invoke the shell with those options or allow them to be specified.

修改 PS1

PS(Prompt Sign): 是指命令提示符,要想修改这个命令提示符。修改 ~/.bashrc 文件中的 PS1 即可。PS1是主要的提示符设置,在ubuntu一般为:
${debian_chroot:+($debian_chroot)}\u@\h:\w\$

具体的提示符,按分类含义如下:
  主要信息:
  /u 当前登录用户名
  /h 当前计算机名称(譬如ubuntu)
  /H 当前计算机的域名全程,譬如(ubuntu.ubuntu.com)
  /w 当前目录的完整路径。家目录会以~代替
  /W 利用basename取得工作目录名称,所以只会列出最后一个目录
  /$ 一般用户为$,root用户为>
  时间显示
  /t 当前时间(24小时制,HH:MM:SS 分别代表 小时:分钟:秒)
  /T 当前时间(12小时制)
  /@ 当前时间(AM/PM显示)
  /d 当前日期
  Shell信息:
  /v Bash版本
  /V Bash的发布版本号
  /S Shell名称
  /! Bash命令的历史编号
  /j job序号
  /l Shell的终端名称

vim ~/.bashrc

root@Ubuntu:~# vim ~/.bashrc 

PS1 修改为 [${debian_chroot:+($debian_chroot)}\u@\h:\W]\$

重新加载bash配置文件

root@Ubuntu:~# source .bashrc 
[root@Ubuntu:~]# 
[root@Ubuntu:~]# 

验证

重启后验证命令提示符不会变

[root@Ubuntu:~]# reboot
...
[root@Ubuntu:~]# 

* 命令提示符还可以修改别的配置,譬如颜色显示等等

Responses
  1. It's an awesome article designed for all the online users; they will obtain advantage from it I am sure.

    Reply
  2. I don't even know how I ended up here, but I thought this post was great. I don't know who you are but definitely you're going to a famous blogger if you are not already ;) Cheers!

    Reply
  3. Hmm is anyone else encountering problems with the images on this blog loading? I'm trying to determine if its a problem on my end or if it's the blog. Any responses would be greatly appreciated.

    Reply
  4. After I initially commented I seem to have clicked the -Notify me when new comments are added- checkbox and from now on every time a comment is added I recieve 4 emails with the exact same comment. There has to be a way you are able to remove me from that service? Thanks a lot!

    Reply
  5. Thanks for another wonderful post. Where else may anybody get that type of info in such an ideal manner of writing? I have a presentation subsequent week, and I'm at the search for such information.

    Reply
  6. I am now not certain where you're getting your info, but good topic. I needs to spend some time learning much more or figuring out more. Thank you for fantastic information I used to be on the lookout for this information for my mission.

    Reply