标签:bashrc
引子
刚才碰见了,其实一直有这个想法,想搞清楚系统下每一个文件都是干啥的。看的懂每一个系统自带的脚本是啥子意思。这次又碰见了这个文件我打算就从它开始,读懂它(/etc/bashrc)都干了什么。
1 # /etc/bashrc 2 3 # System wide functions and aliases 4 # Environment stuff goes in /etc/profile 5 6 # It‘s NOT a good idea to change this file unless you know what you 7 # are doing. It‘s much better to create a custom.sh shell script in 8 # /etc/profile.d/ to make custom changes to your environment, as this 9 # will prevent the need for merging in future updates. 10 11 # are we an interactive shell? 12 if [ "$PS1" ]; then 13 if [ -z "$PROMPT_COMMAND" ]; then 14 case $TERM in 15 xterm*) 16 if [ -e /etc/sysconfig/bash-prompt-xterm ]; then 17 PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm 18 else 19 PROMPT_COMMAND=‘printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"‘ 20 fi 21 ;; 22 screen) 23 if [ -e /etc/sysconfig/bash-prompt-screen ]; then 24 PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen 25 else 26 PROMPT_COMMAND=‘printf "\033]0;%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"‘ 27 fi 28 ;; 29 *) 30 [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default 31 ;; 32 esac 33 fi 34 # Turn on checkwinsize 35 shopt -s checkwinsize 36 [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ " 37 # You might want to have e.g. tty in prompt (e.g. more virtual machines) 38 # and console windows 39 # If you want to do so, just add e.g. 40 # if [ "$PS1" ]; then 41 # PS1="[\u@\h:\l \W]\\$ " 42 # fi 43 # to your custom modification shell script in /etc/profile.d/ directory 44 fi 45 46 if ! shopt -q login_shell ; then # We‘re not a login shell 47 # Need to redefine pathmunge, it get‘s undefined at the end of /etc/profile 48 pathmunge () { 49 case ":${PATH}:" in 50 *:"$1":*) 51 ;; 52 *) 53 if [ "$2" = "after" ] ; then 54 PATH=$PATH:$1 55 else 56 PATH=$1:$PATH 57 fi 58 esac 59 } 60 61 # By default, we want umask to get set. This sets it for non-login shell. 62 # Current threshold for system reserved uid/gids is 200 63 # You could check uidgid reservation validity in 64 # /usr/share/doc/setup-*/uidgid file 65 if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then 66 umask 002 67 else 68 umask 022 69 fi 70 71 # Only display echos from profile.d scripts if we are no login shell 72 # and interactive - otherwise just process them to set envvars 73 for i in /etc/profile.d/*.sh; do 74 if [ -r "$i" ]; then 75 if [ "$PS1" ]; then 76 . "$i" 77 else 78 . "$i" >/dev/null 2>&1 79 fi 80 fi 81 done 82 83 unset i 84 unset pathmunge 85 fi 86 # vim:ts=4:sw=4
我“纯手工”给系统添加用户时,登陆进去发现有点异样。
[root@hp430G2 shell]# su - hive -bash-4.1$ echo $PS1 \s-\v\$ ..... [hive@hp430G2 ~]$ echo $PS1 [\u@\h \W]\$
造成这个的原因是我复制文件的时候没有操作好,导致了隐藏文件(~/.bashrc)没有执行,该文件运行了另外一个文件(/etc/bashrc)。
尝试分析
前11行是注释,由“空行”分为四段。第一段是该文件的名称、系统位置。第二段是说明部分,“系统中的函数、别名在这里定义”,“环境变量在 /etc/profile 里边定义”。第三段是个温馨提示。第四段是个“调皮话”(就位置分析有可能是下文的,也有可能是上文的;暂且不管)。
12-44
看看12行到44行都干什么了。
本文出自 “小崔的实验笔记” 博客,谢绝转载!
标签:bashrc
原文地址:http://sunnybay.blog.51cto.com/2249903/1854160