Customize your profile during login the linux

来源:互联网 发布:玩网络十三水输惨了 编辑:程序博客网 时间:2024/06/14 00:23

1. PS1

http://www.2cto.com/os/201204/126214.html

Linux环境变量中PS1是很重要的环境变量:
 
PS(Prompt Sign): 是指命令提示符,例如在Fedora 12的终端下:[liutao@liutao ~]$  ,在设定PS1环境变量时,我们需要用到预设的一些特殊符号来设定PS1,下面是鸟哥书上关于BASH的特殊变量。
 
o \d :代表日期,格式为 Weekday Month Date,例如 "Mon Aug 1"
 
o \H :完整的主机名称。举例来说,鸟哥的练习机 linux.dmtsai.tw ,那么这个主机名称就是 linux.dmtsai.tw  www.2cto.com  
 
o  \h :仅取主机名称的第一个名字。以上述来讲,就是 linux 而已, .dmtsai.tw 被省略。
 
o \t :显示时间,为 24 小时格式,如: HH:MM:SS
 
o \T :显示时间,12 小时的时间格式!
 
o \A :显示时间,24 小时格式, HH:MM
 
o \u :目前使用者的账号名称;
 
o \v :BASH 的版本信息;
 
o \w :完整的工作目录名称。家目录会以 ~ 取代;
 
o \W :利用 basename 取得工作目录名称,所以仅会列出最后一个目录名。
 
o \# :下达的第几个指令。
 
o \$ :提示字符,如果是 root 时,提示字符为 # ,否则就是 $ 。
 
Fedora 12下,PS1 内容为:
                     

    
鸟哥书上的例子为:              [root@linux /home/dmtsai 16:50 #12]#
 
                                              PS1='[\u@\h \w \A #\#]\$ '
 
在Fedora 12上的实现:
         

 
在上例中我们看出了,PS1在赋值的时用了单引号(‘),下面我们将讨论下在SHELL变量赋值,先看鸟哥书上的变量赋值:
 
1. 变量与变量内容以等号( = )来连结;
 
2. 等号两边不能直接接空格符;
 
3. 变量名称只能是英文字母与数字,但是数字不能是开头字符;
 
4. 若在赋值中有空格符可以使用双引号或单引号来将变量内容结合起来,但须要特别留意, 双引号内的特殊字符可以保有变量特性,但是单引号内的特殊字符则仅为一般字符;
 
5. 必要时需要以反斜杠(\)来将特殊符号 ( 如 Enter, $, \, 空格符, ' 等 ) 变成一般符号;
 
6. 在一串指令中,还需要藉由其它的指令提供的信息,可以使用引用字符(` command` );(特别特别注意,那个 ` 是键盘上方的数字键 1 左边那个按键,而不是单引号!)  www.2cto.com  
 
7. 若该变量为扩增变量内容时,则需以双引号及 $变量名称 如: "$PATH":/home继续累加内容;
 
8. 若该变量需要在其它子程序执行,则需要以 export 来使变量变成环境变量, 如export PATH
 
9. 通常大写字符为系统预设变量,自行设定变量可以使用小写字符,方便判断 ( 纯粹依照使用者兴趣与嗜好 ) ;
 
10. 取消变量的方法为: unset 变量名称。
 
那么在PS1的设置中,因为中间有空格,所以我们需要用单双引号,没有与需要用到命令的结果,所以不需要引用。而我们选择单引号是为了让PS1直接接受纯字符,而不是在SHELL中有特殊意义的字符,或者以$开头的变量,所以我们用单引号即可,当让也可以用双引号。

http://blog.sina.com.cn/s/blog_6433391301013phx.html

# PS1='自定义内容'

   注意两边的单引号

    示例:PS1='(/u@/H /d /t)/$'

   ----------------------------------------------------------------------------------------------------------------

   PS1:就是用户平时的提示符。

   PS2:第一行没输完,等待第二行输入的提示符。

   Linux系统提示符是用系统变量PS1来定义的。一般系统默认的形式是:[username@host 工作目录]$.

    用echo$PS1能够得到PS1的值,即PS1="[/u@/h /w]"/$

   登录后能够更改PS1的显示样式,但是当退出重启登录进入系统后,样式又变成系统默认的样式了,假如要完全改变他的样式,只能从配置文档中改。

   PS是在用户根目录下的.bash_profile中定义的。

   

    #.bash_profile

    # Get thealiases and functions

    if [ -f~/.bashrc ]; then

    .~/.bashrc

    fi

    # Userspecific environment and startup programs

   #以下是设定的PS1的值

   PS1="[/u@/h /w]/$"

   PATH=$PATH:$HOME/bin

   #使用export把PS1输出,以使他能够在子shell中生效,这会造成ROOT用户的也采用此样式

    #exportPS1 要慎用

    exportPATH

    unsetUSERNAME

   下面简单说说环境下默认的特别符号所代表的意义:

    /d:代表日期,格式为weekday month date,例如:"Mon Aug 1"

    /H:完整的主机名称。例如:我的机器名称为:fc4.linux,则这个名称就是fc4.linux

    /h:仅取主机的第一个名字,如上例,则为fc4,.linux则被省略

    /t:显示时间为24小时格式,如:HH:MM:SS

    /T:显示时间为12小时格式

    /A:显示时间为24小时格式:HH:MM

    /u:当前用户的账号名称

    /v:BASH的版本信息

    /w:完整的工作目录名称。家目录会以 ~代替

    /W:利用basename取得工作目录名称,所以只会列出最后一个目录

    /#:下达的第几个命令

    /$:提示字符,假如是root时,提示符为:# ,普通用户则为:$

   ==================================================


   我们能够通过配置PS1变量使提示符成为彩色。在PS1中配置字符序列颜色的格式为:

   /[/e[F;Bm/]

   其中``F''为字体颜色,编号30~37;``B''为背景色,编号40~47。

   可通过``/e[0m''关闭颜色输出;特别的,当B为1时,将显示加亮加粗的文字,周详请看下面的颜色表和代码表。

    颜色表

    前景 背景颜色
   ---------------------------------------
    30 40黑色
    31 41色
    32 42色
    33 43色
    34 44色
    35 45紫色
    36 46青色
    37 47 白色

    代码意义
   -------------------------
    0 OFF

    1高亮显示

    4underline

    5 闪烁

    7反白显示

    8 不可见


   假如想要配置终端提示符的样式只要把$PS1在~/.bahrc指定即可比,比如我的配置如下:

    配置一:

   PS1="/[/e[32m/][/u@/h /w]$/[/e[m/]"

    exportPS1

    效果:

   [LinuxSong@test ~]$

    配置二:

    if [$TERM = 'linux' ]; then
    exportPS1="[/[/e[36;1m/]/u@/h /[/e[31;1m/]/w/[/e[32;1m/]]>"
    zhcon--utf8
    clear
    elif [ $TERM= "xterm" ]; then
    exportPS1="[/[/e[31;1m/]/w/e[0m]> "
    fi

===========================

===========================

\d :代表日期,格式为weekday month date,例如:"Mon Aug 1"
\H :完整的主机名称。例如:我的机器名称为:fc4.linux,则这个名称就是fc4.linux
\h :仅取主机的第一个名字,如上例,则为fc4,.linux则被省略
\t :显示时间为24小时格式,如:HH:MM:SS
\T :显示时间为12小时格式
\A :显示时间为24小时格式:HH:MM
\u :当前用户的账号名称
\v :BASH的版本信息
\w :完整的工作目录名称。家目录会以 ~代替
\W :利用basename取得工作目录名称,所以只会列出最后一个目录
\# :下达的第几个命令
\$ :提示字符,如果是root时,提示符为:# ,普通用户则为:$

   CentOS中关于这些:
\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 isinserted into the prompt string; an empty format results in alocale-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 atilde
\W    the basename of the current working directory, with $HOMEabbreviated 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 charactercorresponding to the octal number nnn
\\    a backslash
\[    begin a sequence of non-printing characters, which could be used toembed a terminal control sequence into the prompt
\]    end a sequence of non-printing characters


   于是可以根据这些指定自己喜欢的命令提示符格式:

PS1="\u@\h:\w\$ "
   但是这样设置之后显示的提示符是没有颜色的,如果希望加入颜色设置,可以这样:

PS1="\e[1;32m\u\e[m\e[1;33m@\e[m\e[1;35m\h\e[m:\w\$"
   此时,在终端中的显示应该是彩色的提示符了,其中的\e[1;xxm和\e[m就是改变终端字体颜色的部分,前者是修改颜色,后者是恢复到正常显示。但是这时还会有问题,在提示符中输入第一行时,回车换行不正常,导致显示很混乱,因此,除了这个修改之外,还需要将全部非打印字符用专用的bash 转义序列 "\[" 和 "\]" 括起来。这两个序列通知 bash,被括起来的字符不占用行上的任何空间,这样就使自动换行能够继续正常工作。最终版本如下(可解决带颜色提示符的不换行问题):

PS1="\[\e[1;32m\]\u\[\e[m\]\[\e[1;33m\]@\[\e[m\]\[\e[1;35m\]\h\[\e[m\]:\w\$ "


#user definedexport PS1='\u@\h: \w>'

So, the promote PS1 as below:

>cd workspace/sonora@sonora-Vostro-270s: ~/workspace>

2. kinds of profiles but which one works for you?

a)  Login shell: bash, $HOME/.bash_profile, $HOME/.bashrc.

           ".bash_profile" is invoked when the user login the box.

          ".bashrc" is invoked when the user starts a new terminal.

b)  Login shell: ksh, $HOME/.profile, $HOME/.krc

             As much as the same bash shell.



原创粉丝点击