配置Bash Shell和系统配置文件

Bash Shell的配置与变量

  • Linux(UNIX)用户可根据需要重新配置系统

  • 最常用重新配置Bash Shell的方法如下:

    1. 利用局域变量来设定Bash Shell
    2. 通过别名和函数来设定Bash Shell
    3. 通过set命令来设定Bash Shell
    4. 通过环境变量设定Bash Shell中其他命令和应用程序
  • shell变量是内存中一个命了名的临时存储区

  • 在其中可以存放数字或字符等信息

  • shell变量的特性如下:

    • shell变量分为两种类型、即局部变量和环境变量
    • 局部变量只能在当前的工作环境(shell)中使用
    • 环境变量即可在当前shell中使用、也会传给它的所有子shell
  • 显示shell变量名和值使用如下两个命令:

    1. 使用set命令显示所有的变量
    2. 使用env命令只显示环境变量
[dag@dog ~]$ set | more
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete
:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="1" [2]="2" [3]="2" [4]="release" [5]="x86_64-redhat-
linux-gnu")
BASH_VERSION='4.1.2(2)-release'
COLORS=/etc/DIR_COLORS
COLORTERM=gnome-terminal
COLUMNS=80
CVS_RSH=ssh
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-OlCMoNPXFc,guid=805ef93b5edcbb1
2e77e291400000046
DESKTOP_SESSION=gnome
DIRSTACK=()
DISPLAY=:0.0
EUID=500
GDMSESSION=gnome
GDM_KEYBOARD_LAYOUT=us
GDM_LANG=zh_CN.UTF-8
GIO_LAUNCHED_DESKTOP_FILE=/usr/share/applications/gnome-terminal.desktop
GIO_LAUNCHED_DESKTOP_FILE_PID=3292
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GNOME_KEYRING_PID=2855
--More--
[5]+  Stopped                 set | more
[dag@dog ~]$ env | more
ORBIT_SOCKETDIR=/tmp/orbit-dag
HOSTNAME=dog.super.com
GIO_LAUNCHED_DESKTOP_FILE_PID=3292
IMSETTINGS_INTEGRATE_DESKTOP=yes
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
XDG_SESSION_COOKIE=9300d649ab64d01fa9a5376500000008-1590027640.651057-1776179669
GTK_RC_FILES=/etc/gtk/gtkrc:/home/dag/.gtkrc-1.2-gnome2
WINDOWID=41943043
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
IMSETTINGS_MODULE=IBus
USER=dag
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd
=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;4
2:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:
*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:
*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31
:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01
;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bm
p=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:
*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=
--More--
[6]+  Stopped                 env | more

通过局部变量设定Shell

  • 通过创建shell变量或修改变量值设定shell
  • Linux中通常习惯用大写字母作为shell变量名
  • 创建shell局部变量的方法:变量名=变量的值
    • DOG1_COLOR=black、DOG1_COLOR为shell变量名、black为变量的值
  • 要提取shell变量中的值在变量之前冠以$符号:
    • Echo $DOG1_COLOR
  • 用以下组合命令列出所有DOG开始的shell变量
    • set | grep DOG
[dag@dog ~]$ DOG1_COLOR=black
[dag@dog ~]$ set | more
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete
:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="1" [2]="2" [3]="2" [4]="release" [5]="x86_64-redhat-
linux-gnu")
BASH_VERSION='4.1.2(2)-release'
COLORS=/etc/DIR_COLORS
COLORTERM=gnome-terminal
COLUMNS=80
CVS_RSH=ssh
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-OlCMoNPXFc,guid=805ef93b5edcbb1
2e77e291400000046
DESKTOP_SESSION=gnome
DIRSTACK=()
DISPLAY=:0.0
DOG1_COLOR=black
EUID=500
--More--
Most commands optionally preceded by integer argument k.  Defaults in brackets.
Star (*) indicates argument becomes new default.
-------------------------------------------------------------------------------
<space>                 Display next k lines of text [current screen size]
z                       Display next k lines of text [current screen size]*
<return>                Display next k lines of text [1]*
d or ctrl-D             Scroll k lines [current scroll size, initially 11]*
q or Q or <interrupt>   Exit from more
s                       Skip forward k lines of text [1]
f                       Skip forward k screenfuls of text [1]
b or ctrl-B             Skip backwards k screenfuls of text [1]
'                       Go to place where previous search started
=                       Display current line number
/<regular expression>   Search for kth occurrence of regular expression [1]
n                       Search for kth occurrence of last r.e [1]
!<cmd> or :!<cmd>       Execute <cmd> in a subshell
v                       Start up /usr/bin/vi at current line
ctrl-L                  Redraw screen
:n                      Go to kth next file [1]
:p                      Go to kth previous file [1]
:f                      Display current file name and line number
.                       Repeat previous command
-------------------------------------------------------------------------------
没有前一个常规表达式
[7]+  Stopped                 set | more
[dag@dog ~]$ echo $DOG1_COLOR
black
[dag@dog ~]$ DOG2_COLOR=grey
[dag@dog ~]$ set | grep DOG
DOG1_COLOR=black
DOG2_COLOR=grey
[dag@dog ~]$ set | grep LINES
LINES=24
[dag@dog ~]$ set | grep PS1
PS1='[\u@\h \W]\$ '
[dag@dog ~]$ env | grep PS1

局部变量PS1

  • PS1变量主要设置Bash 提示符所显示的信息
  • 可将一些换码序列插入到PS1变量中、它们成为提示信息的一部分、常用换码序列如下:
  • \d:系统当前的日期、d应该是date的第1个字母
  • \t:系统当前的时间、t应该是time的第1个字母
  • \h:简短形式的主机名、h应该是host(主机)的第1个字母
  • \u:当前用户名、u应该是user的第1个字母
  • \w:当前的工作目录、w应是working directory的第1个字母
  • !:当前命令的历史编号、!为执行历史命令的第1个字符
  • $:如果是普通用户显示$、而如果是root用户显示#
  • \l:显示shell终端设备的基本名、l应该是line的第1个字母
[dag@dog ~]$ echo $PS1
[\u@\h \W]\$
[dag@dog ~]$ set | grep PS1
PS1='[\u@\h \W]\$ '
[dag@dog ~]$ PS1='[\u@\h \W]\$ '
[dag@dog ~]$ PS1='[\u@\h \w TTY\1 \d \t \!]\$'
[dag@dog ~ TTY\1 四 5月 21 01:14:36 273]$echo $DOG1_COLOR
black
[dag@dog ~ TTY\1 四 5月 21 01:16:13 274]$
[dag@dog ~ TTY\1 四 5月 21 01:16:19 274]$

别名的用法及设定

  • 使用alias命令为history命令创建别名h
  • 使用alias命令为rm -i创建一个名为del的别名
  • 使用不带任何参数的alias命令列出所有的别名
  • 取消别名命令为:unalias 别名的名字
    alias h=history
    alias del=‘rm -i’
    [dog@dog wolf]$ alias
    alias del=‘rm -i’
    alias dir=‘ls -laF’
    alias h=‘history’
    ……
[dag@dog ~]$ alias
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

[dag@dog ~]$ alias dir='ls -laF'

[dag@dog ~]$ alias dir
alias dir='ls -laF'
[dag@dog ~]$ dir
总用量 25088
drwx------. 37 dag  dag     4096 5月  21 05:26 ./
drwxr-xr-x.  6 root root    4096 5月  17 23:12 ../
drwxrwxr-x.  2 dag  dag     4096 3月  29 23:52 .abrt/
drwxrwxr-x.  2 dag  dag     4096 5月  20 00:15 arch/
-rw-rw-r--.  1 dag  dag        0 5月  20 00:13 arch.gz
-rw-rw-r--.  1 dag  dag  6174720 5月  20 00:04 arch.tar
-rw-rw-r--.  1 dag  dag  6104748 5月  20 00:17 arch.tar.bz2
-rw-rw-r--.  1 dag  dag  6107364 5月  20 00:16 arch.tar.gz
drwxrwxr-x.  2 dag  dag     4096 5月  19 23:25 babydog/
drwxrwxr-x.  2 dag  dag     4096 5月  21 05:26 backup/
-rw-------.  1 dag  dag     3203 5月  21 05:26 .bash_history
-rw-r--r--.  1 dag  dag       18 10月 15 2014 .bash_logout
-rw-r--r--.  1 dag  dag      176 10月 15 2014 .bash_profile
-rw-r--r--.  1 dag  dag      124 10月 15 2014 .bashrc
drwxrwxr-x.  2 dag  dag     4096 5月  13 23:53 boydog/
drwxr-xr-x.  6 dag  dag     4096 5月  20 22:22 .cache/
-rw-rw-r--.  1 dag  dag     1208 5月  13 23:24 cal2038
-rw-rw-r--.  1 dag  dag     2128 5月  13 23:29 cal3009
drwx------.  2 dag  dag     4096 5月  17 23:51 .comments/
drwxr-xr-x.  7 dag  dag     4096 3月  29 23:52 .config/
drwx------.  3 dag  dag     4096 3月  29 23:52 .dbus/
-rw-r--r--.  1 dag  dag       32 5月  20 22:20 .dmrc
-rw-r--r--.  1 root root 4724150 3月  15 2018 dog.JPG
-rw-r--r--.  1 dag  dag      500 10月 10 2013 .emacs
-rw-r--r--.  1 root root   13015 5月  16 07:11 epel-release-6-8.noarch.rpm
-rw-------.  1 dag  dag       16 3月  29 23:52 .esd_auth
-rw-r--r--.  1 root root 1433498 3月  15 2018 flowers.JPG
drwxr-xr-x.  2 dag  dag     4096 5月  16 08:08 .fontconfig/
-rw-r--r--.  1 root root    1950 3月  15 2018 game.txt
drwx------.  4 dag  dag     4096 5月  20 22:20 .gconf/
drwx------.  2 dag  dag     4096 5月  16 23:08 .gconfd/
drwxr-xr-x. 10 dag  dag     4096 5月  16 23:08 .gnome2/
drwx------.  2 dag  dag     4096 3月  29 23:53 .gnome2_private/
drwxrwxr-x.  3 dag  dag     4096 3月  29 23:52 .gnote/
drwx------.  2 dag  dag     4096 5月  20 22:20 .gnupg/
drwxrwxr-x.  2 dag  dag     4096 3月  29 23:52 .gstreamer-0.10/
-rw-rw-r--.  1 dag  dag      180 5月  20 22:20 .gtk-bookmarks
drwx------.  2 dag  dag     4096 3月  29 23:52 .gvfs/
-rw-------.  1 dag  dag     3720 5月  20 22:20 .ICEauthority
-rw-r--r--.  1 dag  dag      789 5月  20 22:20 .imsettings.log
-rw-r--r--.  1 root root    4720 3月  15 2018 learning.txt
-rw-rw-r--.  1 dag  dag     1208 5月  13 23:28 lists
-rw-rw-r--.  1 dag  dag     1208 5月  14 03:34 lists200
drwxr-xr-x.  3 dag  dag     4096 3月  29 23:52 .local/
drwxr-xr-x.  6 dag  dag     4096 5月  20 22:22 .mozilla/
drwxrwxr-x.  2 dag  dag     4096 5月  16 23:37 mumdog/
drwxr-xr-x.  2 dag  dag     4096 3月  29 23:52 .nautilus/
-rw-rw-r--.  1 dag  dag       36 5月  16 10:29 news
-rw-r--r--.  1 root root  869219 3月  15 2018 NewZealand.JPG
drwxrw----.  3 dag  dag     4096 5月  21 00:21 .pki/
drwx------.  2 dag  dag     4096 3月  29 23:52 .pulse/
-rw-------.  1 dag  dag      256 3月  29 23:52 .pulse-cookie
drwx------.  2 dag  dag     4096 5月  13 05:04 .ssh/
drwx------.  3 dag  dag     4096 5月  13 04:21 .thumbnails/
-rw-------.  1 dag  dag     4344 5月  21 05:26 .viminfo
drwxrwxr-x.  2 dag  dag     4096 5月  19 23:40 wolf/
-rw-------.  1 dag  dag     4003 5月  21 05:26 .xsession-errors
-rw-------.  1 dag  dag     2145 5月  20 00:17 .xsession-errors.old
drwxr-xr-x.  2 dag  dag     4096 3月  29 23:52 公共的/
drwxr-xr-x.  2 dag  dag     4096 3月  29 23:52 模板/
drwxr-xr-x.  2 dag  dag     4096 3月  29 23:52 视频/
drwxr-xr-x.  2 dag  dag     4096 3月  29 23:52 图片/
drwxr-xr-x.  2 dag  dag     4096 3月  29 23:52 文档/
drwxr-xr-x.  2 dag  dag     4096 3月  29 23:52 下载/
drwxr-xr-x.  2 dag  dag     4096 3月  29 23:52 音乐/
drwxr-xr-x.  2 dag  dag     4096 5月  13 04:57 桌面/
[dag@dog ~]$ cd wolf
[dag@dog wolf]$ dir
总用量 8
drwxrwxr-x.  2 dag dag 4096 5月  19 23:40 ./
drwx------. 37 dag dag 4096 5月  21 05:26 ../
-rw-rw-r--.  1 dag dag    0 5月  16 22:45 dog1.wolf
-rw-rw-r--.  1 dag dag    0 5月  16 22:45 dog2.wolf
-rw-rw-r--.  1 dag dag    0 5月  16 22:45 dog3.wolf
-rw-rw-r--.  1 dag dag    0 5月  16 22:47 dog3.wolf.boy
-rw-rw-r--.  1 dag dag    0 5月  16 22:46 dog.wolf.boy
-rw-rw-r--.  1 dag dag    0 5月  16 22:46 dog.wolf.girl
[dag@dog wolf]$ h
bash: h: command not found
[dag@dog wolf]$ alias h=history
[dag@dog wolf]$ h
    1  su root
    2  chkconfig telnet --list
    3  su root
    4  su rooot
    5  su root
    6  whoami
    7  who am i
    8  ssh user@localhost
    9  who
   10  w
   11  users
   12  tty
  ......

[dag@dog wolf]$ alias del='rm-i'
[dag@dog wolf]$ del dog3.wolf.boy
bash: rm-i: command not found
[dag@dog wolf]$ alias del='rm -i'
[dag@dog wolf]$ del dog3.wolf.boy
rm:是否删除普通空文件 "dog3.wolf.boy"?n
[dag@dog wolf]$ dir *.boy
-rw-rw-r--. 1 dag dag 0 5月  16 22:47 dog3.wolf.boy
-rw-rw-r--. 1 dag dag 0 5月  16 22:46 dog.wolf.boy
[dag@dog wolf]$ alias
alias del='rm -i'
alias dir='ls -laF'
alias h='history'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[dag@dog wolf]$ unalias dir
[dag@dog wolf]$ unalias h
[dag@dog wolf]$ unalias del
[dag@dog wolf]$ alias
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[dag@dog wolf]$ alias info='hostname;whoami;date'
[dag@dog wolf]$ info
dog.super.com
dag
2020年 05月 21日 星期四 06:13:10 EDT

利用set进行shell的设置

  • 许多shell参数可通过set -o命令来设定
  • 列出全部可通过set -o命令设置的参数及默认值
  • 使用set命令重新设置noclobber参数的值
  • 列出noclobber参数的当前值
    set -o | more
    set -o noclobber
    [dog@dog wolf]$ set -o | grep noclob
    noclobber on
[dag@dog wolf]$ cd -
/home/dag

[dag@dog ~]$ set -o | more
allexport          off
braceexpand        on
emacs              on
errexit            off
errtrace           off
functrace          off
hashall            on
histexpand         on
history            on
ignoreeof          off
interactive-comments    on
keyword            off
monitor            on
noclobber          off
noexec             off
noglob             off
nolog              off
notify             off
nounset            off
onecmd             off
physical           off
pipefail           off
posix              off
privileged         off
verbose            off
vi                 off
xtrace             off

[dag@dog ~]$ cd wolf

[dag@dog wolf]$ cp dog.wolf.baby.boy
cp: 在"dog.wolf.baby.boy" 后缺少了要操作的目标文件
请尝试执行"cp --help"来获取更多信息。
[dag@dog wolf]$ cp dog.wolf.baby boy
cp: 无法获取"dog.wolf.baby" 的文件状态(stat): 没有那个文件或目录
[dag@dog wolf]$ ls
dog1.wolf  dog2.wolf  dog3.wolf  dog3.wolf.boy  dog.wolf.boy  dog.wolf.girl
[dag@dog wolf]$ cat dog.wolf.boy
[dag@dog wolf]$ ls
dog1.wolf  dog2.wolf  dog3.wolf  dog3.wolf.boy  dog.wolf.boy  dog.wolf.girl
[dag@dog wolf]$ cd -
/home/dag
[dag@dog ~]$ ls
arch          backup                       flowers.JPG   news            图片
arch.gz       boydog                       game.txt      NewZealand.JPG  文档
arch.tar      cal2038                      learning.txt  wolf            下载
arch.tar.bz2  cal3009                      lists         公共的          音乐
arch.tar.gz   dog.JPG                      lists200      模板            桌面
babydog       epel-release-6-8.noarch.rpm  mumdog        视频
[dag@dog ~]$ cd boydog
[dag@dog boydog]$ ls
cal2012  cal3009
[dag@dog boydog]$ cd -
/home/dag
[dag@dog ~]$ cd babydog
[dag@dog babydog]$ ls
baby      baby.kg   cal2038   dog_wolf.baby  lists
baby2     baby.sex  cal3009   letters        wolf.baby
baby.age  cal2012   dog.baby  letters.upper  wolf_dog.baby
[dag@dog babydog]$ cat baby
Age:3 months
Weight: 8kg
Gender:F
[dag@dog babydog]$ cp baby /home/dag/wolf/boy
[dag@dog babydog]$ cd -
/home/dag
[dag@dog ~]$ cd wolf
[dag@dog wolf]$ ls
boy        dog2.wolf  dog3.wolf.boy  dog.wolf.girl
dog1.wolf  dog3.wolf  dog.wolf.boy
[dag@dog wolf]$ boy
bash: boy: command not found
[dag@dog wolf]$ cat boy
Age:3 months
Weight: 8kg
Gender:F

[dag@dog wolf]$ cat boy
Age:3 months
Weight: 8kg
Gender:F

[dag@dog wolf]$ la -l >& boy

[dag@dog wolf]$ cat boy
bash: la: command not found

[dag@dog wolf]$ la -l boy
bash: la: command not found

[dag@dog wolf]$ cat boy
bash: la: command not found

[dag@dog wolf]$ cat /home/dag/babydog/baby /home/dag/wolf/boy
Age:3 months
Weight: 8kg
Gender:F
bash: la: command not found

[dag@dog wolf]$ cp  /home/dag/babydog/baby /home/dag/wolf/boy

[dag@dog wolf]$ cat boy
Age:3 months
Weight: 8kg
Gender:F

[dag@dog wolf]$ set -o noclobber

[dag@dog wolf]$ set -o
allexport          off
braceexpand        on
emacs              on
errexit            off
errtrace           off
functrace          off
hashall            on
histexpand         on
history            on
ignoreeof          off
interactive-comments    on
keyword            off
monitor            on
noclobber          on
noexec             off
noglob             off
nolog              off
notify             off
nounset            off
onecmd             off
physical           off
pipefail           off
posix              off
privileged         off
verbose            off
vi                 off
xtrace             off

[dag@dog wolf]$ set -o | grep noclob
noclobber          on

[dag@dog wolf]$ la -l >&boy
bash: boy: cannot overwrite existing file

[dag@dog wolf]$ la -l > boy
bash: boy: cannot overwrite existing file

[dag@dog wolf]$ cat boy
Age:3 months
Weight: 8kg
Gender:F

[dag@dog wolf]$ date >> boy

[dag@dog wolf]$ cat boy
Age:3 months
Weight: 8kg
Gender:F
2020年 05月 21日 星期四 22:19:43 EDT

[dag@dog wolf]$ set -o | egrep 'emacs|vi'
emacs              on
privileged         off
vi                 off

[dag@dog wolf]$ set -o vi

[dag@dog wolf]$ set -o | egrep 'emacs|vi'
emacs              off
privileged         off
vi                 on

[dag@dog wolf]$ set -o emacs

[dag@dog wolf]$ set -o | egrep 'emacs|vi'
emacs              on
privileged         off
vi                 off

将局部变量转换成环境变量

  • 当用户创建了一个变量之后,这个变量只能在该用户目前工作的shell环境中使用
  • 一旦离开了当前的shell环境,该变量就失效
  • 只有环境变量才能不仅在当前的shell环境中生效、而且还可以在它的所有子shell中生效
  • 将自定义的变量升级成环境变量的命令:
    • export 变量名
    • 如export DOG1_COLOR
    • export DOG2_COLOR=grey

当一个变量不再需要时可用unset命令取消

  • unset DOG2_COLOR
  • 在子shell中, unset命令取消当前shell中的变量、而其他shell中的变量不受影响
  • 在主(parent) shell中, unset命令不但取消了当前shell中的变量、而且也取消它的所有子shell中的变量
[dag@dog ~]$ set | grep DOG

[dag@dog ~]$ DOG1_COLOR=black

[dag@dog ~]$ set | grep DOG
DOG1_COLOR=black

[dag@dog ~]$ su cat
密码:

[cat@dog dag]$ set | grep DOG

[cat@dog dag]$ exit
exit

[dag@dog ~]$ export DOG1_COLOR

[dag@dog ~]$ env | grep DOG
DOG1_COLOR=black
[dag@dog ~]$ su cat
密码:

[cat@dog dag]$ set | grep DOG
DOG1_COLOR=black

[cat@dog dag]$ env | grep DOG
DOG1_COLOR=black

[cat@dog dag]$ exit
exit

[dag@dog ~]$ export DOG2_COLOR=grey

[dag@dog ~]$ env | grep DOG2
DOG2_COLOR=grey

[dag@dog ~]$ su cat
密码:

[cat@dog dag]$ env | grep DOG2
DOG2_COLOR=grey

[cat@dog dag]$ unset DOG2_COLOR

[cat@dog dag]$ env | grep DOG
DOG1_COLOR=black

[cat@dog dag]$ exit
exit

[dag@dog ~]$ env | grep DOG
DOG2_COLOR=grey
DOG1_COLOR=black

[dag@dog ~]$ unset DOG1_COLOR
[dag@dog ~]$ set | grep DOG
DOG2_COLOR=grey
_=DOG1_COLOR

[dag@dog ~]$ unset DOG1_COLOR

[dag@dog ~]$ env | grep DOG
DOG2_COLOR=grey

[dag@dog ~]$ su cat
密码:

[cat@dog dag]$ env | grep DOG
DOG2_COLOR=grey

常用的环境变量

  • 在Linux系统中有许多预设的环境变量
  • 经常使用的环境变量及操作环境的命令如下:
    • HOME:用户家目录的路径
    • PWD:用户当前的工作目录
    • LANG:标识程序将要使用的默认语言
    • TERM:用户登录终端的类型
    • reset:当屏幕崩溃,重新设置终端的命令(不是变量)
    • PATH:可执行文件(命令)搜索路径
    • which:定位并显示可执行文件所在路径的命令(不是变量)
    • SHELL:用户登录shell的路径。
    • USER:用户的用户名。
      DISPLAY:X显示器的名字
[cat@dog dag]$ cd -
bash: cd: OLDPWD not set

[cat@dog dag]$ su dag
密码:

[dag@dog ~]$ env | grep HOME
HOME=/home/dag

[dag@dog ~]$ su - root
密码:

[root@dog ~]# env | grep HOME
HOME=/root

[root@dog ~]# echo $HOME
/root

[root@dog ~]# env | grep PWD
PWD=/root

[root@dog ~]# exit
logout

[root@dog ~]# exit
logout

[dag@dog ~]$ env | grep PWD
PWD=/home/dag

[dag@dog ~]$ cd wolf

[dag@dog wolf]$ env | grep PWD
PWD=/home/dag/wolf
OLDPWD=/home/dag

[dag@dog wolf]$ env | grep LANG
LANG=zh_CN.UTF-8
GDM_LANG=zh_CN.UTF-8

[dag@dog wolf]$ cd -
/home/dag

[dag@dog ~]$ env | grep LANG
LANG=zh_CN.UTF-8
GDM_LANG=zh_CN.UTF-8

[dag@dog ~]$ env | grep TERM
TERM=xterm
COLORTERM=gnome-terminal

[dag@dog ~]$ env | grep TERM
TERM=xterm
COLORTERM=gnome-terminal

[dag@dog ~]$ env | grep PATH
PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/dag/bin
WINDOWPATH=1

shell启动脚本和登录shell

  • Shell启动脚本的作用包含以下4点:
    1. 通过在启动脚本中设置局部变量或运行set命令来设置shell
    2. 通过在启动脚本文件中建立环境变量设置其他程序
    3. 在启动脚本文件中创建(启用)别名
    4. 在启动脚本文件中定义系统启动时要执行的程序

login shell执行的启动脚本和顺序

  • 当一个用户登录Linux时,登录shell按如下顺序执行所需的脚本:
    1. 执行/etc/profile启动脚本、在/etc/profile中会调用/etc/profile.d目录下的所有启动脚本
    2. 执行/.bash_profile启动脚本、在/.bash_profile启动脚本中又会调用/.bashrc启动脚本、而/.bashrc启动脚本又将调用/etc/bashrc启动脚本
  • Linux为每个用户自动创建.bash_profile和.bashrc脚本文件,这两个文件存放在用户的家目录中
[dag@dog ~]$ ls -l /etc/profile.d
总用量 72
-rw-r--r--. 1 root root 1150 5月   2 2018 colorls.csh
-rw-r--r--. 1 root root 1172 5月   2 2018 colorls.sh
-rw-r--r--. 1 root root   92 10月 10 2013 cvs.csh
-rw-r--r--. 1 root root   78 10月 10 2013 cvs.sh
-rw-r--r--. 1 root root  192 4月  30 2018 glib2.csh
-rw-r--r--. 1 root root  192 4月  30 2018 glib2.sh
-rw-r--r--. 1 root root   58 4月   9 2019 gnome-ssh-askpass.csh
-rw-r--r--. 1 root root   70 4月   9 2019 gnome-ssh-askpass.sh
-rw-r--r--. 1 root root 1745 5月  24 2019 lang.csh
-rw-r--r--. 1 root root 2706 5月  24 2019 lang.sh
-rw-r--r--. 1 root root  123 8月  16 2014 less.csh
-rw-r--r--. 1 root root  121 8月  16 2014 less.sh
-rw-r--r--. 1 root root  976 10月 12 2011 qt.csh
-rw-r--r--. 1 root root  912 10月 12 2011 qt.sh
-rw-r--r--. 1 root root 2142 1月  11 2017 udisks-bash-completion.sh
-rw-r--r--. 1 root root  105 7月  18 2019 vim.csh
-rw-r--r--. 1 root root  269 7月  18 2019 vim.sh
-rw-r--r--. 1 root root  169 10月 13 2011 which2.sh
[dag@dog ~]$ tail -8 /etc/profile
        else
            . "$i" >/dev/null 2>&1
        fi
    fi
done


unset i
unset -f pathmunge
[dag@dog ~]$ tail -12  /etc/profile
for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null 2>&1
        fi
    fi
done


unset i
unset -f pathmunge

[dag@dog ~]$ su - root
密码:

[root@dog ~]# cp /etc/profile profile.bak

[root@dog ~]# ls
anaconda-ks.cfg  install.log.syslog  公共的  视频  文档  音乐
install.log      profile.bak         模板    图片  下载  桌面

[root@dog ~]# vi /etc/profile
# /etc/profile
echo '1. /etc/profile is running !!!'
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc


[root@dog ~]# exit
logout

[dag@dog ~]$ vi .bash_profile
# .bash_profile
echo "2. ~/.bash profile is running !!!"
# Get the aliases and functions

[dag@dog ~]$ vi .bashrc
# .bashrc
echo "3. ~/.bashrc is running !!!"
# Source global definitions

[dag@dog ~]$ vi /etc/bashrc
[dag@dog ~]$ su - root
密码:

[root@dog ~]# vi /etc/bashrc
# /etc/bashrc
# .bashrc
echo "4. /etc/bashrc is running !!!"
# Source global definitions

退出重新登录dag用户和cat用户
[dag@dog ~]$ su - cat
密码:
1. /etc/profile is running !!!
4. /etc/bashrc is running !!!

[cat@dog ~]$ su - dag
密码:
1. /etc/profile is running !!!
2. ~/.bash profile is running !!!
3. ~/.bashrc is running !!!
4. /etc/bashrc is running !!!

Non-login shell 执行的启动脚本和顺序

  • Non-login shell不执行/etc/profile启动脚本
  • 也不执行~/.bash_profile启动脚本
  • 当用户以Non-login shell登录Linux系统时,
  • 将首先执行/.bashrc脚本文件、而/.bashrc脚本将调用/etc/bashrc脚本文件
  • 当执行完了这两个启动脚本文件之后,Non-login shell才会执行/etc/profile.d目录中全部相关的脚本文件
#显示的结果非常清楚的标明Non-login shell 没有执行etc/profile和~/.bash_profile启动脚本
[cat@dog ~]$  su dag
密码:
3. ~/.bashrc is running !!!
4. /etc/bashrc is running !!!

[dag@dog cat]$ pwd
/home/cat

[dag@dog cat]$ whoami
dag

#使用bash启动一个新的shell
[dag@dog cat]$ bash
3. ~/.bashrc is running !!!
4. /etc/bashrc is running !!!

#使用sh命令发现shell将不会执行~/.bashrc和/etc/bashrc启动脚本,执行完命令以后
系统的提示符变成了Bourn Shell的提示符sh-4.1$,为后面的操作方便,使用exit退回原来的Bash Shell
[dag@dog cat]$ sh
sh-4.1$ exit
exit

/etc/profile文件 和/etc.prpfile.d目录

  • Login shell启动时确保执行启动脚本/etc/profile

  • 在这个脚本文件中设置的一些重要变量为:

    • PATH:预设可执行文件或命令的搜索路径。
    • USER:用户登录时使用的用户名。
    • LOGNAME:其值为$USER。
    • HOSTNAME:所使用的主机名。
    • MAIL:存放用户电子邮件的邮箱、一个ASCII码文件
    • HISTSIZE:历史记录的行数。
    • INPUTRC变量:它指向/etc/inputrc文件、该文件是 一个 ASCII码文件、其中存放的是针对键盘热键设置的信息
  • 在/etc/profile.d目录中存放的是一些应用程序所需的启动脚本

  • 这些脚本包括颜色、语言、less、vim及which等命令的一些附加设置

  • 因为在/etc/profile中使用for循环语句调用了这些脚本,所以这些脚本才能够被自动执行

  • 这些脚本是用来设置一些变量和运行一些初始化过程的

[dag@dog ~]$ cat /etc/profile | grep USER
    USER="`/usr/bin/id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL


[dag@dog ~]$ id
uid=500(dag) gid=500(dag) 组=500(dag),504(friends) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

[dag@dog ~]$ id -u
500

[dag@dog ~]$ id -un
dag

[dag@dog ~]$ echo $USER
dag

[dag@dog ~]$ echo $LOGNAME
dag

[dag@dog ~]$ echo $MAIL
/var/spool/mail/dag

[dag@dog ~]$ file /var/spool/mail/dag
/var/spool/mail/dag: empty

[dag@dog ~]$ cat /var/spool/mail/dag

[dag@dog ~]$ cd /etc/profile.d

[dag@dog profile.d]$ ls
colorls.csh  glib2.sh               less.csh                   vim.csh
colorls.sh   gnome-ssh-askpass.csh  less.sh                    vim.sh
cvs.csh      gnome-ssh-askpass.sh   qt.csh                     which2.sh
cvs.sh       lang.csh               qt.sh
glib2.csh    lang.sh                udisks-bash-completion.sh

[dag@dog profile.d]$ file less.sh
less.sh: ASCII text

[dag@dog profile.d]$ cat less.sh
# less initialization script (sh)
[ -x /usr/bin/lesspipe.sh ] && export LESSOPEN="${LESSOPEN-||/usr/bin/lesspipe.sh %s}"

~/.bash_profile和~/.bashrc及其他一些系统文件

  • /.bash_profile和/.bashrc这两个脚本文件中主要是存放用户自己的一些设定、其中包括了用户自己定义的变量和别名
  • 如登录时需执行某些将把输出信息传送到屏幕上的指令,应该将这些指令放在/.bash_profile文件中,而不要放在/.bashrc文件中
  • /etc/bashrc脚本文件中的信息是全局性的,其中包括了一些全系统使用的函数和别名的设定,如umask的设定

其他:

  • 环境变量的设定并不放在~/.bash_profile文件中、而是放在/etc/profile文件中
  • ~/.bash_logout脚本文件也是存放在用户的家目录中、每当用户退出系统时就会运行该脚本文件
  • ~/.bash_logout主要作用是用户退出系统时自动运行某些程序,如自动备份一些重要并在用户登录后更改过的文件、及删除没用的临时文件等
  • Linux系统支持多种语言、而这些有关语言信息的变量是由/etc/ sysconfig/i18n文件维护的
  • ~/.bash_history文件存放用户使用过的命令、每个命令一行
  • 每当用户登录bash之后,bash就会立即将这个文件中的所有历史命令读入内存
  • 正因为如此,一个用户才能查看到他使用过的历史命令
  • 使用如下的命令获取历史命令的总数:
    [dog@dog ~]$ history | wc -l
    999
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章