unit9 config the bash shell
.local variables
.aliases and functions
.the set command
.environment variables
variables: A variable is a label that has a value
.used to config the shell or other programs
.variables are resided in memory
.two types :local and environment
.local varibales are used only by the shell
.environment variables are passed onto other commamd
.display variables and values using
.set to display all variables
.env to display environment variables
set |more 查看系统中所有的变量
env | more ===环境 变量
configuring the shell: local variables
.Data in shell scripts and environment settings stored in variables
.conventionally all upper-case
.setting variable value
$ FAV_COLOR=blue
set |more 查看
.to retrieve variable value.use $ before variable name
echo $FAV_COLOR
blue
4,common local variables
.histfilesize determines how many commands to be saved
in the history file on logout
.columns sets the width of the terminal
.lines sets the height of the terminal
5,ps1 local variable
. ps1 sets the prompt
.\d today is date
.\h short hostname
.\t current time
.\u user name
.\w current working directory
.\! the history number of current command
.\$ shows $ if you are a none-privileged user and a # if you are a privileged user
.\l the basename of the shell is terminal device name
[root@mycentos ~]# echo $PS1
[\u@\h \W]\$
[root@mycentos ~]#
PS1='[\u@\h \w \l \d \t \!]\$'
[root@mycentos ~ 0 Tue Apr 28 03:47:37 342]#
PS1='[\u@\h \w tty\l \d \t \!]\$'
[root@mycentos ~ tty0 Tue Apr 28 03:48:19 343]#
6,Aliases
.Aliases let you create shortcuts to commands
$ alias if='ls -laF'
.USE alias by itself to see all set aliases
.use alias followde by an alias name to see alias value $alias if
alias if='ls -laF'
alias 显示系统中的别名
alias if
.set -o noclobber
.set -o vi
[root@mycentos ~]#ls -l doc1
total 28
-rw-r--r-- 1 root root 1 Apr 28 03:54 test1.txt
-rw-r--r-- 1 root root 60 Apr 27 23:16 test2.txt
-rw-r--r-- 1 root root 42 Apr 27 23:16 test3.txt
-rw-r--r-- 1 root root 0 Apr 27 23:14 text1.txt
[root@mycentos ~]#cal > doc1/test1.txt
[root@mycentos ~]#cat doc1/test1.txt
April 2009
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
[root@mycentos ~]#ca >& doc1/text1.txt 正确和错误的信息传入到text1.txt
[root@mycentos ~]#cat doc1/text1.txt
-bash: ca: command not found
[root@mycentos ~]#set -o noclobber=====保留原来的内容
[root@mycentos ~]#cal > doc1/text1.txt
-bash: doc1/text1.txt: cannot overwrite existing file
[root@mycentos ~]#cal >> doc1/text1.txt
[root@mycentos ~]#cat doc1/text1.txt
-bash: ca: command not found
April 2009
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
config commands: Environment -----全局变量Variables============================================环境变量
.shell variables exist only in current shell environment
.Environment variables passed to subshells
.shell variables can be exported into environment
$ export variable name ===升级成环境变量
[root@mycentos ~]#set |more============================查看新建的变数:FAV_COLOR=blue
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="2" [2]="25" [3]="1" [4]="release" [5]="i686-redhat-linu
x-gnu")
BASH_VERSION='3.2.25(1)-release'
COLORS=/etc/DIR_COLORS
COLUMNS=83
CVS_RSH=ssh
DIRSTACK=()
EUID=0
FAV_COLOR=blue==========================存在的
GROUPS=()
G_BROKEN_FILENAMES=1
HISTFILE=/root/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/root
HOSTNAME=mycentos
HOSTTYPE=i686
IFS=$' \t\n'
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LINES=38
LOGNAME=root
LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:
or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01
;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01
;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*
.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:
*.xpm=01;35:*.png=01;35:*.tif=01;35:'
MACHTYPE=i686-redhat-linux-gnu
MAIL=/var/spool/mail/root
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin
:/usr/sbin:/usr/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=2865
PS1='[\u@\h \w]\$'
PS2='> '
PS4='+ '
PWD=/root
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
:noclobber
SHLVL=1
SSH_AUTH_SOCK=/tmp/ssh-GuqqAj2865/agent.2865
SSH_CLIENT='192.168.1.100 1273 22'
SSH_CONNECTION='192.168.1.100 1273 192.168.1.199 22'
SSH_TTY=/dev/pts/0
TERM=vt100
UID=0
USER=root
_=clear
consoletype=pty
You have new mail in /var/spool/mail/root
[root@mycentos ~]#useradd u1
[root@mycentos ~]#useradd u2
[root@mycentos ~]#su u1
[u1@mycentos root]$ whoami==================================到u1用户上
u1
[u1@mycentos root]$ set | more =============查看有没有FAV_COLOR
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="2" [2]="25" [3]="1" [4]="release" [5]="i686-redhat-linu
x-gnu")
BASH_VERSION='3.2.25(1)-release'
COLORS=/etc/DIR_COLORS
COLUMNS=83
CVS_RSH=ssh ==============================没有
DIRSTACK=()
EUID=500
GROUPS=()
G_BROKEN_FILENAMES=1
HISTFILE=/home/u1/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/home/u1
HOSTNAME=mycentos
HOSTTYPE=i686
IFS=$' \t\n'
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LINES=38
LOGNAME=u1
LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:
or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01
;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01
;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*
.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:
*.xpm=01;35:*.png=01;35:*.tif=01;35:'
MACHTYPE=i686-redhat-linux-gnu
MAIL=/var/spool/mail/root
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin
:/usr/sbin:/usr/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=14394
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PWD=/root
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=2
SSH_AUTH_SOCK=/tmp/ssh-GuqqAj2865/agent.2865
SSH_CLIENT='192.168.1.100 1273 22'
SSH_CONNECTION='192.168.1.100 1273 192.168.1.199 22'
SSH_TTY=/dev/pts/0
TERM=vt100
UID=500
USER=u1
_=whoami
consoletype=pty
[u1@mycentos root]$ exit
exit
[root@mycentos ~]#export FAV_COLOR=============================把此变数导出为环境变量
[root@mycentos ~]#su u1 ==============================再次进入u1用户查看
[u1@mycentos root]$ set |more
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="2" [2]="25" [3]="1" [4]="release" [5]="i686-redhat-linu
x-gnu")
BASH_VERSION='3.2.25(1)-release'
COLORS=/etc/DIR_COLORS
COLUMNS=83
CVS_RSH=ssh
DIRSTACK=()
EUID=500
FAV_COLOR=blue =================================已经出现了此变数
GROUPS=()
G_BROKEN_FILENAMES=1
HISTFILE=/home/u1/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/home/u1
HOSTNAME=mycentos
HOSTTYPE=i686
IFS=$' \t\n'
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LINES=38
LOGNAME=u1
LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:
or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01
;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01
;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*
.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:
*.xpm=01;35:*.png=01;35:*.tif=01;35:'
MACHTYPE=i686-redhat-linux-gnu
MAIL=/var/spool/mail/root
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin
:/usr/sbin:/usr/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=19533
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PWD=/root
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=2
SSH_AUTH_SOCK=/tmp/ssh-GuqqAj2865/agent.2865
SSH_CLIENT='192.168.1.100 1273 22'
SSH_CONNECTION='192.168.1.100 1273 192.168.1.199 22'
SSH_TTY=/dev/pts/0
TERM=vt100
UID=500
USER=u1
_=/etc/bashrc
consoletype=pty
[u1@mycentos root]$ exit
exit
[root@mycentos ~]#export A=1 =====================将变数A导出为环境变量
[root@mycentos ~]#set |more
A=1=======================================root用户中已经存在了
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="2" [2]="25" [3]="1" [4]="release" [5]="i686-redhat-linu
x-gnu")
BASH_VERSION='3.2.25(1)-release'
COLORS=/etc/DIR_COLORS
COLUMNS=83
CVS_RSH=ssh
DIRSTACK=()
EUID=0
FAV_COLOR=blue
GROUPS=()
G_BROKEN_FILENAMES=1
HISTFILE=/root/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/root
HOSTNAME=mycentos
HOSTTYPE=i686
IFS=$' \t\n'
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LINES=38
LOGNAME=root
LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:
or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01
;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01
;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*
.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:
*.xpm=01;35:*.png=01;35:*.tif=01;35:'
MACHTYPE=i686-redhat-linux-gnu
MAIL=/var/spool/mail/root
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin
:/usr/sbin:/usr/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=2865
PS1='[\u@\h \w]\$'
PS2='> '
PS4='+ '
PWD=/root
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
:noclobber
SHLVL=1
SSH_AUTH_SOCK=/tmp/ssh-GuqqAj2865/agent.2865
SSH_CLIENT='192.168.1.100 1273 22'
SSH_CONNECTION='192.168.1.100 1273 192.168.1.199 22'
SSH_TTY=/dev/pts/0
TERM=vt100
UID=0
USER=root
_=A
consoletype=pty
[root@mycentos ~]#su u1 =================切换到u1用户查看
[u1@mycentos root]$ set | more
A=1 ==================也出现了
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="2" [2]="25" [3]="1" [4]="release" [5]="i686-redhat-linu
x-gnu")
BASH_VERSION='3.2.25(1)-release'
COLORS=/etc/DIR_COLORS
COLUMNS=83
CVS_RSH=ssh
DIRSTACK=()
EUID=500
FAV_COLOR=blue
GROUPS=()
G_BROKEN_FILENAMES=1
HISTFILE=/home/u1/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/home/u1
HOSTNAME=mycentos
HOSTTYPE=i686
IFS=$' \t\n'
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LINES=38
LOGNAME=u1
LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:
or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01
;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01
;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*
.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:
*.xpm=01;35:*.png=01;35:*.tif=01;35:'
MACHTYPE=i686-redhat-linux-gnu
MAIL=/var/spool/mail/root
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin
:/usr/sbin:/usr/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=25759
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PWD=/root
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=2
SSH_AUTH_SOCK=/tmp/ssh-GuqqAj2865/agent.2865
SSH_CLIENT='192.168.1.100 1273 22'
SSH_CONNECTION='192.168.1.100 1273 192.168.1.199 22'
SSH_TTY=/dev/pts/0
TERM=vt100
UID=500
USER=u1
_=/etc/bashrc
consoletype=pty
[u1@mycentos root]$ exit
exit
unset FAV_COLOR ==================================
在普通用户中删除变数 不会影响到其他的用户变数
但是如果在root下删除变数的话,将涉及到其他的用户
常见的环境变量:==========================
.HOME 用户的主目录
.LANG 设置用户的显示语言
.PWD 当前工作目录
TERM 变数
.TERM
.reset
path 环境变数 ======系统默认路径
.path 系统预设路径
[root@mycentos ~]#echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@mycentos ~]#PATH=$PATH:.
[root@mycentos ~]#echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:.
. which
HOw the shell expands the command line-----------------
.A simplification
.split the line into shell words
' " | & ; ()
.function and alias
.curly brace string expansion
{,}
.tilde 家目录
.file rediretion
.> >> < << 2< 2>
shell starup scripts===========================
作用:
.scripts of commands executed at login
.user include:
.config the shell by setting local variables or running the set commands
.config other programs through envrionment variables
.establish aliases
.run programs on startup
LOGin shells=========登陆触发的SHELL
.login shells
./etc/profile for i in /etc/profile.d/*.sh ; do ===========================
. $i
fi
done
./etc/profile.d =======================
.~/.bash_profile =====================
[root@mycentos ~]#cat ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
unset USERNAME
.~/.bashrc==========================
[root@mycentos ~]#cat ~/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
. /etc/bashrc==============
编辑每个文件 加上 echo /etc/profile
echo ~/.bash_profile
echo ~/.bashrc
echo ./etc/bashrc
non-login shells ==================
.~/.bashrc 启动时执行
./etc/bashrc
./etc/progile.d
编辑 vi ~/.bash_profile
vi ~/.bashrc =========
16 /etc/profile=========================
.login shell is started run
.only runs for login shells
.non-login shells do not invoke this script
.this script will set a series of variables including PATH,USER,LOGNAME ,MAIL ,HOSTNAME,HISTSIZE,AND INPUTC
17 /etc/profile.d
.应用程序用到得启动脚本
.建立变数和初始化的程序
18 ~/.bash_profile and ~/.bashrc
存放用户自已的设定,包含变数,别名
date
19 ~/.bash_logout
存放在home 目录中
登出所使用的应用程序
make backups of files
delete temporary files
display date and time of logout
本文出自 “www.51cto.com” 博客,谢绝转载!