- 论坛徽章:
- 0
|
ubuntun在BASH启动过程中,以交互式登入并且终端为xterm时,PS1的值就是这样的。
1、BASH 的召唤过程。
INVOCATION
A login shell is one whose first character of argument zero is a -, or one started with the --login option.
An interactive shell is one started without non-option arguments and without the -c option whose standard input
and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1
is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.
The following paragraphs describe how bash executes its startup files. If any of the files exist but cannot be
read, bash reports an error. Tildes are expanded in file names as described below under Tilde Expansion in the
EXPANSION section.
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it
first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it
looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from
the first one that exists and is readable. The --noprofile option may be used when the shell is started to
inhibit this behavior.
When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.
When an interactive shell that is not a login shell is started, bash reads and executes commands from
/etc/bash.bashrc and ~/.bashrc, if these files exist. This may be inhibited by using the --norc option. The
--rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc and
~/.bashrc.
When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV
in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to
read and execute. Bash behaves as if the following command were executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search for the file name.
If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as
closely as possible, while conforming to the POSIX standard as well. When invoked as an interactive login
shell, or a non-interactive shell with the --login option, it first attempts to read and execute commands from
/etc/profile and ~/.profile, in that order. The --noprofile option may be used to inhibit this behavior. When
invoked as an interactive shell with the name sh, bash looks for the variable ENV, expands its value if it is
defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh
does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect.
A non-interactive shell invoked with the name sh does not attempt to read any other startup files. When
invoked as sh, bash enters posix mode after the startup files are read.
When bash is started in posix mode, as with the --posix command line option, it follows the POSIX standard for
startup files. In this mode, interactive shells expand the ENV variable and commands are read and executed
from the file whose name is the expanded value. No other startup files are read.
Bash attempts to determine when it is being run with its standard input connected to a a network connection, as
if 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 rshd does not generally invoke the shell
with those options or allow them to be specified.
If the shell is started with the effective user (group) id not equal to the real user (group) id, and the -p
option is not supplied, no startup files are read, shell functions are not inherited from the environment, the
SHELLOPTS, BASHOPTS, CDPATH, and GLOBIGNORE variables, if they appear in the environment, are ignored, and the
effective user id is set to the real user id. If the -p option is supplied at invocation, the startup behavior
is the same, but the effective user id is not reset.
2、我们就以交互式登入为例跟踪其过程。
file: /etc/profile
- 1 # /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
- 2 # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
- 3
- 4 if [ -d /etc/profile.d ]; then
- 5 for i in /etc/profile.d/*.sh; do
- 6 if [ -r $i ]; then
- 7 . $i
- 8 fi
- 9 done
- 10 unset i
- 11 fi
- 12
- 13 if [ "$PS1" ]; then
- 14 if [ "$BASH" ]; then
- 15 PS1='\u@\h:\w\$ '
- 16 if [ -f /etc/bash.bashrc ]; then
- 17 . /etc/bash.bashrc
- 18 fi
- 19 else
- 20 if [ "`id -u`" -eq 0 ]; then
- 21 PS1='# '
- 22 else
- 23 PS1='$ '
- 24 fi
- 25 fi
- 26 fi
- 27
- 28 umask 022
复制代码 显然此时PS1未定义,只是source /etc/profile.d/*.sh, 在此目录下有个
file: /etc/profile.d/speechd-user-port.sh
- 1 export SPEECHD_PORT=$(expr 6560 + $(getent passwd $USER | cut -f 3 -d :))
复制代码 这句话的意思就是用户的uid + 6560赋值给SPEECHED_PORT
file: ~/.profile
- 1 # ~/.profile: executed by the command interpreter for login shells.
- 2 # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
- 3 # exists.
- 4 # see /usr/share/doc/bash/examples/startup-files for examples.
- 5 # the files are located in the bash-doc package.
- 6
- 7 # the default umask is set in /etc/profile; for setting the umask
- 8 # for ssh logins, install and configure the libpam-umask package.
- 9 #umask 022
- 10
- 11 # if running bash
- 12 if [ -n "$BASH_VERSION" ]; then
- 13 # include .bashrc if it exists
- 14 if [ -f "$HOME/.bashrc" ]; then
- 15 . "$HOME/.bashrc"
- 16 fi
- 17 fi
- 18
- 19 # set PATH so it includes user's private bin if it exists
- 20 if [ -d "$HOME/bin" ] ; then
- 21 PATH="$HOME/bin:$PATH"
- 22 fi
复制代码 source ~/.bashrc 然后添加了用户自己的bin目录到PATH
file: ./bashrc
- 1 # ~/.bashrc: executed by bash(1) for non-login shells.
- 2 # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
- 3 # for examples
- 4
- 5 # If not running interactively, don't do anything
- 6 [ -z "$PS1" ] && return
- 7
- 8 # don't put duplicate lines in the history. See bash(1) for more options
- 9 # ... or force ignoredups and ignorespace
- 10 HISTCONTROL=ignoredups:ignorespace
- 11
- 12 # append to the history file, don't overwrite it
- 13 shopt -s histappend
- 14
- 15 # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
- 16 HISTSIZE=1000
- 17 HISTFILESIZE=2000
- 18
- 19 # check the window size after each command and, if necessary,
- 20 # update the values of LINES and COLUMNS.
- 21 shopt -s checkwinsize
- 22
- 23 # make less more friendly for non-text input files, see lesspipe(1)
- 24 [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
- 25
- 26 # set variable identifying the chroot you work in (used in the prompt below)
- 27 if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
- 28 debian_chroot=$(cat /etc/debian_chroot)
- 29 fi
- 30
- 31 # set a fancy prompt (non-color, unless we know we "want" color)
- 32 case "$TERM" in
- 33 xterm-color) color_prompt=yes;;
- 34 esac
- 35
- 36 # uncomment for a colored prompt, if the terminal has the capability; turned
- 37 # off by default to not distract the user: the focus in a terminal window
- 38 # should be on the output of commands, not on the prompt
- 39 #force_color_prompt=yes
- 40
- 41 if [ -n "$force_color_prompt" ]; then
- 42 if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
- 43 # We have color support; assume it's compliant with Ecma-48
- 44 # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
- 45 # a case would tend to support setf rather than setaf.)
- 46 color_prompt=yes
- 47 else
- 48 color_prompt=
- 49 fi
- 50 fi
- 51
- 52 if [ "$color_prompt" = yes ]; then
- 53 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
- 54 else
- 55 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
- 56 fi
- 57 unset color_prompt force_color_prompt
- 58
- 59 # If this is an xterm set the title to user@host:dir
- 60 case "$TERM" in
- 61 xterm*|rxvt*)
- 62 PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
- 63 ;;
- 64 *)
- 65 ;;
- 66 esac
- 67
- 68 # enable color support of ls and also add handy aliases
- 69 if [ -x /usr/bin/dircolors ]; then
- 70 test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
- 71 alias ls='ls --color=auto'
- 72 #alias dir='dir --color=auto'
- 73 #alias vdir='vdir --color=auto'
- 74
- 75 alias grep='grep --color=auto'
- 76 alias fgrep='fgrep --color=auto'
- 77 alias egrep='egrep --color=auto'
- 78 fi
- 79
- 80 # some more ls aliases
- 81 alias ll='ls -alF'
- 82 alias la='ls -A'
- 83 alias l='ls -CF'
- 84
- 85 # Alias definitions.
- 86 # You may want to put all your additions into a separate file like
- 87 # ~/.bash_aliases, instead of adding them here directly.
- 88 # See /usr/share/doc/bash-doc/examples in the bash-doc package.
- 89
- 90 if [ -f ~/.bash_aliases ]; then
- 91 . ~/.bash_aliases
- 92 fi
- 93
- 94 # enable programmable completion features (you don't need to enable
- 95 # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
- 96 # sources /etc/bash.bashrc).
- 97 if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
- 98 . /etc/bash_completion
- 99 fi
复制代码 把PS1设置部分抓出来分析- 52 if [ "$color_prompt" = yes ]; then
- 53 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
- 54 else
- 55 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
- 56 fi
- 57 unset color_prompt force_color_prompt
- 58
- 59 # If this is an xterm set the title to user@host:dir
- 60 case "$TERM" in
- 61 xterm*|rxvt*)
- 62 PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
- 63 ;;
- 64 *)
- 65 ;;
- 66 esac
复制代码 这个debian_chroot解释如下
Using The chroot Environments on Debian Machines
To use the chroot environments set up on the Debian machines run the dchroot program. In each chroot, there is a file /etc/debian_chroot, the contents of which will tell you which chroot you are in.
大致意思是用来在多个根文件系统的切换,以表示你在哪个根文件系统。
基本上我们只挂一个根文件系统,所以debian_chroot空。
${debian_chroot:+($debian_chroot)}的含义
表达式替换var=${str:+expr} str没设置var=expr, str为空,则var为空,str有值var=str
这样${debian_chroot:+($debian_chroot)}就为空了
\[\033[01;32m这类东西表示终端颜色转义
52-56行意思就很明确是否提示符要带颜色显示。中间的${debian_chroot:+($debian_chroot)}表示在那个根文件系统。
在后面的62行代码开始,表示xterm就是图形终端了。在ubuntu一般就是伪终端了输入who看到的pts什么的就是了。
一般X windows在ubuntu我看到的是在tty7加载。
\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a
这一大串就是你打开终端的标题.\[\e]0;就是终端颜色取消,你打开的终端标题栏就看到了。
\e]0;title\a是标题表示模式匹配
只须用您希望其出现在 xterm 标题栏中的文字替换子串 "title" 即可,
您能够将 bash 转义序列插入标题栏中,他将用户名、主机名和当前工作目录显示在标题栏中。
顺便提一句,确保用 "\[" 和 "\]" 将您的标题栏序列括起来(因为就终端而言,这个序列是非打印序列)。 |
|