星期五, 二月 02, 2007

bash点滴

bash的配置文件有三个,.bash_profile .bashrc .bash_logout,它们一般就在用户home目录下。没有的情况下会使用系统默认的/etc/bashrc

1: .bash_profile:用户Login时执行,设置个人环境。
.bash_profile的修改要在用户重新Login的时候才会生效。也可以使用source命令或者.命令执行,使之生效。
bash向前兼容C shell的.login(bash中更名为.bash_login)和Bourne shell和Korn shell的.profile配置文件。当你Login的时候,这三个文件只有一个会被执行,配置文件搜索顺序:.bash_profile---> .bash_login---> .profile

2: .bashrc:用户调用shell时执行,设置调用shell的运行环境。 (bash的环境配置文件)
提供将用户的Login环境和subshell环境区分开来的灵活性。
  用户特定的aliases和functions在.bashrc里定义。避免subshell运行舒畅,尽可能把所有的定义放在这里
用户自己的特定配置和需要启动的程序在.bash_profile里给出

3: .bash_logout:顾名思义,用户Logout时执行,做一些清理工作。



Alias 为命令创建合适的名字
格式:alias name=command
bash在执行命令前,会将所有的aliases进行文本置换,所以通配符及特殊字符不能包含在alias名字中
对文件夹的名字进行alias,需要一点技巧。alias anim='/work/fisrt/second/third/fourth' cd anim的时候,系统会报错,找不到这个文件或者文件夹。需要再加一个alias cd='cd ' 或者 shopt -s cdable_vars 允许cd的参数是变量
不建议用很多aliases,shell脚本和function能够提供更强大的功能

Option
格式:set -o | set +o +表示关闭option -表示打开option(The reason for this incongruity is that the dash (-) is the conventional UNIX way of specifying options to a command, while the use of + is an afterthought)
或者使用shopt -psuqo

varible
声明:variable=value 使用:$variable 删除:unset variable(此时只有当set -o nounset的时候,才有用。因为对于bash来说,所有未定义变量的值为null(即等于''),nounset 选项使得bash在碰到一个未定义的变量时,提示错误)

在命令行执行命令时,命令的执行优先级
1: Aliases
2: Keywords such as function if for
3: Functions
4: Built-ins like cd and type
5: Scirpt and Executable programs in directories listed in the PATH