zoukankan      html  css  js  c++  java
  • Shell终端配置

    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
    •  : newline
    •  : carriage return
    • s : the name of the shell, the basename of $0 (the portion following the final slash)
    •  : 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 $ 
    • nn : 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 	 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.  

      

      

  • 相关阅读:
    Python 不同数据类型比较
    计算机外语收集
    d3.js学习-联系力学图
    d3.js学习11
    d3.js学习10
    d3.js学习9
    d3.js学习8
    [springboot jpa] [bug] Could not open JPA EntityManager for transaction
    [spring cloud feign] [bug] 使用对象传输get请求参数
    [ethereum源码分析](5) 创建新账号
  • 原文地址:https://www.cnblogs.com/happygirl-zjj/p/6185581.html
Copyright © 2011-2022 走看看