3.1.1. 系统配置文件
3.1.1.1. /etc/profile
当使用—login选项交互调用或以sh调用时,Bash 读取/etc/profile指令。通常会设置 shell 变量PATH、USER、MAIL、HOSTNAME和HISTSIZE。
在某些系统上,umask值在/etc/profile中配置;在其他系统上,此文件包含指向其他配置文件的指针,例如:
/etc/inputrc, 系统范围的 Readline 初始化文件,您可以在其中配置命令行铃声样式。
/etc/profile.d 目录,其中包含配置特定程序的系统范围行为的文件。
您要应用于所有用户环境的所有设置都应在此文件中。它可能看起来像这样:
\# /etc/profile
# System wide environment and startup programs, for login setup
PATH=$PATH:/usr/X11R6/bin
# No core files by default
ulimit -S -c 0 > /dev/null 2>&1
USER="\`id -un\`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
HOSTNAME=\`/bin/hostname\`
HISTSIZE=1000
# Keyboard, bell, display style: the readline config file:
if \[ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" \]; then
INPUTRC=/etc/inputrc
fi
PS1="\\u@\\h \\W"
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC PS1
# Source initialization files for specific programs (ls, vim, less, ...)
for i in /etc/profile.d/\*.sh ; do
if \[ -r "$i" \]; then
. $i
fi
done
# Settings for program initialization
source /etc/java.conf
export NPX\_PLUGIN\_PATH="$JRE\_HOME/plugin/ns4plugin/:/usr/lib/netscape/plugins"
PAGER="/usr/bin/less"
unset i
此配置文件设置了一些基本的 shell 环境变量以及用户在其 Web 浏览器中运行 Java 和/或 Java 应用程序所需的一些变量。请参阅第 3.2 节。
有关在此文件中使用if条件的更多信息,请参见第 7 章;第 9 章讨论了诸如for构造之类的循环。
Bash source包含供一般或个人使用的示例配置文件。这些和上面示例需要应用更改才能在您的环境中工作!(也就是 source profile)
3.1.1.2. /etc/bashrc
在提供多种类型的 shell 的系统上,最好将 Bash 特定的配置放在这个文件中,因为/etc/profile也可以被其他 shell 读取,例如 Bourne shell。通过为不同类型的 shell 拆分配置文件,可以防止由不理解 Bash 语法的 shell 生成的错误。在这种情况下,用户的~/.bashrc可能指向/etc/bashrc,以便在登录时将其包含在 shell 初始化过程中。
您可能还会发现系统上的/etc/profile仅包含 shell 环境和程序启动设置,而/etc/bashrc包含 shell 函数和别名的系统范围定义。/etc/bashrc文件可能在/ etc/profile或单个用户 shell 初始化文件中被引用。
source包含bashrc文件,或者您可以在/usr/share/doc/bash-2.05b/startup-files中找到副本。这是Bash 文档附带的bashrc的一部分:
alias ll='ls -l'
alias dir='ls -ba'
alias c='clear'
alias ls='ls --color'
alias mroe='more'
alias pdw='pwd'
alias sl='ls --color'
pskill()
{
local pid
pid=$(ps -ax | grep $1 | grep -v grep | gawk '{ print $1 }')
echo -n "killing $1 (process $pid)..."
kill -9 $pid
echo "slaughtered."
}
3.1.2. 个人用户配置文件
没有这些文件?!
默认情况下,这些文件可能不在您的主目录中;如果需要,创建它们。
3.1.2.1. ~/.bash_profile
这是单独配置用户环境的首选配置文件。在此文件中,用户可以添加额外的配置选项或更改默认设置:
franky~> **cat .bash\_profile**
#################################################################
# #
# .bash\_profile file #
# #
# Executed from the bash shell when you log in. #
# #
#################################################################
source ~/.bashrc
source ~/.bash\_login
case "$OS" in
IRIX)
stty sane dec
stty erase
;;
# SunOS)
# stty erase
# ;;
\*)
stty sane
;;
esac
此配置退格字符以在不同操作系统上登录。除此之外,还会读取用户的.bashrc和.bash_login 。
3.1.2.2. ~/.bash_login
此文件包含仅在您登录系统时执行的特定设置。在示例中,我们使用它来配置umask值并在登录时显示已连接用户的列表。该用户还获得了日历:
#######################################################################
# #
# Bash\_login file #
# #
# commands to perform from the bash shell at login time #
# (sourced from .bash\_profile) #
# #
#######################################################################
# file protection
umask 002 # all to me, read to group and others
# miscellaneous
w
cal \`date +"%m"\` \`date +"%Y"\`
在没有~/.bash_profile的情况下,将读取此文件。
3.1.2.3. ~/.profile
在没有~/.bash_profile和~/.bash_login的情况下,读取~/.profile 。它可以保持相同的配置,然后其他 shell 也可以访问这些配置。请注意,其他 shell 可能不理解 Bash 语法。
3.1.2.4. ~/.bashrc
今天,使用非登录 shell 更为常见,例如使用 X 终端窗口以图形方式登录时。打开此类窗口时,用户无需提供用户名或密码。发生这种情况时, Bash 会搜索~/.bashrc,因此在登录时读取的文件中也会引用它,这意味着您不必在多个文件中输入相同的设置。
在这个用户的.bashrc中,定义了几个别名,并在读取/etc/bashrc后设置了特定程序的变量:
franky ~> **cat .bashrc**
# /home/franky/.bashrc
# Source global definitions
if \[ -f /etc/bashrc \]; then
. /etc/bashrc
fi
# shell options
set -o noclobber
# my shell variables
export PS1="\\\[\\033\[1;44m\\\]\\u \\w\\\[\\033\[0m\\\] "
export PATH="$PATH:~/bin:~/scripts"
# my aliases
alias cdrecord='cdrecord -dev 0,0,0 -speed=8'
alias ss='ssh octarine'
alias ll='ls -la'
# mozilla fix
MOZILLA\_FIVE\_HOME=/usr/lib/mozilla
LD\_LIBRARY\_PATH=/usr/lib/mozilla:/usr/lib/mozilla/plugins
MOZ\_DIST\_BIN=/usr/lib/mozilla
MOZ\_PROGRAM=/usr/lib/mozilla/mozilla-bin
export MOZILLA\_FIVE\_HOME LD\_LIBRARY\_PATH MOZ\_DIST\_BIN MOZ\_PROGRAM
# font fix
alias xt='xterm -bg black -fg white &'
# BitchX settings
export IRCNAME="frnk"
# THE END
franky ~>
3.1.2.5. ~/.bash_logout
此文件包含注销过程的具体说明。在示例中,终端窗口在注销时被清除。这对于远程连接很有用,它会在关闭它们后留下一个干净的窗口。
franky ~> **cat .bash\_logout**
#######################################################################
# #
# Bash\_logout file #
# #
# commands to perform from the bash shell at logout time #
# #
#######################################################################
clear
franky ~>
3.1.3. 更改 shell 配置文件
对上述任何文件进行更改时,用户必须重新连接到系统或source 配置文件才能使更改生效。通过这种方式,更改将应用于当前的 shell 会话:
Figure 3-1. Different prompts for different users
大多数 shell 脚本在私有环境中执行:变量不会被子进程继承,除非它们被父 shell 导出。获取包含 shell 命令的文件是一种将更改应用到您自己的环境并在当前 shell 中设置变量的方法。
该示例还演示了不同用户对不同提示设置的使用。在这种情况下,红色意味着危险。当你有一个绿色提示时,不要太担心。
注意 source resourcefile和. resourcefile 是一样的。
如果您迷失在所有这些配置文件中,并且发现自己遇到了来源不明的设置,请使用echo语句,就像调试脚本一样;请参阅第 2.3.2 节。您可能会添加这样的行:
echo "Now executing .bash\_profile.."
或者
echo "Now setting PS1 in .bashrc:"
export PS1="\[some value\]"
echo "PS1 is now set to $PS1"
https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_01.html