Shell终端配置

How to: Change / Setup bash custom prompt (PS1)

参考链接:https://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html

Most of us work with a shell prompt. By default most Linux distro displays hostname and current working directory. You can easily customize your prompt to display information important to you. You change look and feel by adding colors. In this small howto I will explain howto setup:
a] Howto customizing a bash shell to get a good looking prompt
b] Configure the appearance of the terminal.
c] Apply themes using bashish
d] Howto pimp out your shell prompt

Prompt is control via a special shell variable. You need to set PS1, PS2, PS3 and PS4 variable. If set, the value is executed as a command prior to issuing each primary prompt.

  • PS1 – The value of this parameter is expanded (see PROMPTING below) and used as the primary prompt string. The default value is \s-\v\$ .
  • PS2 – The value of this parameter is expanded as with PS1 and used as the secondary prompt string. The default is >
  • PS3 – The value of this parameter is used as the prompt for the select command
  • PS4 – The value of this parameter is expanded as with PS1 and the value is printed before each command bash displays during an execution trace. The first character of PS4 is replicated multiple times, as necessary, to indicate multiple levels of indirection. The default is +

查看PS1变量

echo $PS1

输出

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 ‘.’ 仅取主机名称的第一个名字。以 linux.dmtsai.tw来讲,就是 linux 而已, .dmtsai.tw 被省略。
  • \H : the hostname. 完整的主机名称。举例来说,鸟哥的练习机 linux.dmtsai.tw ,那么这个主机名称就是 linux.dmtsai.tw
  • \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 利用 basename 取得工作目录名称,所以仅会列出最后一个目录名。
  • \! : the history number of this command
  • \# : the command number of this command
  • \$ : if the effective UID is 0(root), 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

例1,输入:

PS1="\d \h $"

输出  

例2,输入:

PS1="[\d \t \u@\h:\w ] $ "

输出:

BASH Shell: Change The Color of My Shell Prompt Under Linux or UNIX

参考链接:https://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/

To add colors to the shell prompt use the following export command syntax:
'\e[x;ym $PS1 \e[m'
Where,

  • \e[ : Start color scheme.
  • x;y : Color pair to use (x;y)
  • $PS1 : Your shell prompt variable.
  • \e[m : Stop color scheme.

To set a red color prompt, type the following command:

$ export PS1="\e[0;31m[\u@\h \W]\$ \e[m "

sample output:

A list of color codes

Note: You need to replace digit 0 with 1 to get light color version.

Task: How do I make the prompt setting permanent?

Your new shell prompt setting set by $PS1 is temporary i.e. when you logout setting will be lost. To have it set every time you login to your workstation add above export command to your $HOME/.bash_profile file or $HOME/.bashrc file.

cd
vi .bash_profile

OR

vi $HOME/.bashrc

Append the following line:

export PS1="\e[0;31m[\u@\h \W]\$ \e[m"

Save and close the file.

Putting it all together

Let us say when you login as root/superuser, you want to get visual confirmation using red color prompt. To distinguish between superuser and normal user you use last character in the prompt, if it changes from $ to #, you have superuser privileges. So let us set your prompt color to RED when you login as root, otherwise display normal prompt.

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/bashrc

or

$ sudo gedit /etc/bashrc

Append 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

Close and save the file.  

  

  

Shell终端配置的更多相关文章

  1. Linux的shell终端常用快捷键

    参考: http://www.360doc.com/content/17/0627/09/44797135_666854802.shtml https://linux.cn/article-5660- ...

  2. 全新 Mac 安装指南(编程篇)(环境变量、Shell 终端、SSH 远程连接)

    注:本文专门用于指导对计算机编程与设计(尤其是互联网产品开发与设计)感兴趣的 Mac 新用户,如何在 Mac OS X 系统上配置开发与上网环境,另有<全新 Mac 安装指南(通用篇)>作 ...

  3. Linux下shell颜色配置

    颜色配置涉及以下几个地方(本人常用的):命令提示符,文件及目录名显示,echo -e命令 1.颜色值分为前景色和背景色,颜色码值对应关系如下: Front Back Color 黑 红 绿 黄(棕) ...

  4. 简单快捷好用的vim配置和终端配置推荐

    vim 配置实用spf13-vim,安装方便简单快捷,极力推荐. 另外oh-my-zsh 终端配置很好,与之搭配使用效果更佳. 安装都很简单,一个脚本搞定, 都是在gitHub上开源的,自行搜索,这里 ...

  5. linux之shell终端使用操作快捷键

    所谓的shell终端就是桌面右键里面的打开终端那个终端 敲命令是一件很有趣的事,可是有时候我们会遇到一些很麻烦的事 例如,命令太长导致敲完后一大串字符可读性低,想把vi filename 快速改为ca ...

  6. 【转】更改 shell 终端的默认键绑定为 vi 模式

    我们使用的 shell 终端是 bash,它的默认键绑定方式是 emacs 模式.比如键入 Ctrl+a 光标会停在行首,Ctrl+e 光标会停在行尾等等. 如果希望在终端输入时使用 vi 的模式,比 ...

  7. 【linux】如何退出shell终端

    退出shell终端: exit + 回车即可 清除当前屏幕信息 clear 不过clear只是将之前的命令向上隐藏啦...

  8. linux shell终端打开方式

    前言 Linux操作系统没有Window操作系统界面友好,使用者需要使用命令与系统进行交互,交互媒介为shell终端. 有三种方式可以打开终端: 方法一: 打开新的窗口并打开shell终端,快捷键:c ...

  9. mac shell终端编辑命令行快捷键——行首行尾

    mac shell终端编辑命令行快捷键——行首行尾 ctrl+a //移到行首ctrl+e //移到行尾===========linux系统用============alt+a //移到光标所在单词首 ...

随机推荐

  1. Wordpress基础:文章和页面的区别

    页面: 页面是你可以单独建立一个固定页面,可以作为留言板,或者通知的单页面,发布之后是固定的网址. 页面并不能被分类.亦不能拥有标签,但是它们可以有层级关系.您可将页面附属在另一个页面之下. 对应模板 ...

  2. 《Linux内核分析》第五周 扒开系统调用的三层皮(下)

    [刘蔚然 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] WEEK FIVE( ...

  3. 读源码之RESideMenu

    RESideMenu是github上比较出名的一个开源库,主要是实现侧滑菜单,现在有三千多个star了.效果如下.   据说创意来源于dribbble的一个设计,还是比较好看的.感兴趣的可以去gith ...

  4. SynchronousQueue应用

    SynchronousQueue是无界的,是一种无缓冲的等待队列,但是由于该Queue本身的特性,在某次添加元素后必须等待其他线程取走后才能继续添加:可以认为SynchronousQueue是一个缓存 ...

  5. MSSQL 2012 拒绝了对对象 'extended_properties' (数据库 'mssqlsystemresource',架构 'sys')的 SELECT 权限

    查看数据库的表的时候报如下错误: MSSQL 拒绝了对对象 ) 解决方法: 在数据库里相应的用户权限中,把db_denydatareader的复选框的勾去掉.db_denydatareader是拒绝访 ...

  6. 学习OpenCV——Surf(特征点篇)&flann

    Surf(Speed Up Robust Feature) Surf算法的原理                                                             ...

  7. 【Selenium】1.介绍 Selenium

    本文供学习交流之用,没有商业用途,没有盈利. 完全是我自己为督促自己学习而翻译的.翻译的不好,见谅.来源于:http://www.guru99.com/introduction-to-selenium ...

  8. 7.$a = 'abcdef'; 请取出$a的值并打印出第一个字母

    echo $a[0]; echo $a{0}; echo chr(ord($a));//先输出$a字符串里的第一个字符的ASCII值 再从指定的 ASCII 值返回字符.

  9. JS实时数据运算

    应朋友需要制作的一个小页面 <script type="text/javascript"> function cal(ida,idb,idc,idd) { var nu ...

  10. flexigrid扩展(添加全选,格式化表单)

    1.关于flexigrid的属性这里不多说,百度一大把. 这里主要说一个参数process process参数:编写对某一列进行自定义处理的函数 function 方法名(tddiv,pid){}// ...