Linux入门-终端环境(bash)设置篇

Bash是Debian的默认SHELL,设置终端环境(bash)使Bash更好地为我们服务,我们需定制bash shell环境,这里讲述bash用户环境配置。

bash_profile、.bashrc、和.bash_logout
上面这三个文件是bash shell的用户环境配置文件,位于用户的主目录下。注意bash_profile、.bash_logout并不总是存在,至少Debian中就没有默认创建

一、shell的启动方式
根据shell的启动方式不同,分为登录shell、交互shell和非交互shell,不同方式启动的shell初始化过程是不同的。
1.登录shell(login shell)
当我们输入用户名和密码登录到系统后,所呈现给我们的就是一个登录shell。更确切的说,如果启动bash对话的命令行第0个参数的首字符是”-“的话,那么这是一个登录shell;
可以通过如果命令看到
系统管理员工具包: 充分利用 bashgt; ps -f
还有一种是带有”–login”选项启动的bash对话是一个登录shell。
系统管理员工具包: 充分利用 bashgt; bash –login

2.交互shell(interactive shell)
带有”-i”选项启动的bash对话是一个交互shell。如:
系统管理员工具包: 充分利用 bashgt; bash -i
或者以非选项参数开始,并且不包含”-c”选项启动的bash对话,且它的标准输入和标准出错都是连接到终端的,那么这也是一个交互shell。
系统管理员工具包: 充分利用 bashgt; bash
3.非交互shell
用于启行一个脚本的shell是非交互shell
系统管理员工具包: 充分利用 bashgt; bash my.sh
二、登录shell的初始化文件
/etc/profile;
~/.bash_profile、~/.bash_login和~/.profile三个最先找到的那个;
~/.bash_logout,shell退出时执行。
三、交互shell的初始化
/etc/bash.bashrc
~/.bashrc
四、非交互shell的初始化
环境变量”BASH_ENV”代表的文件。

.bash_profile

其中.bash_profile是最重要的一个配置文件,它在用户每次登录系统时被读取,里面的所有 命令都会被bash执行。.profile(由Bourne Shell和Korn Shell使用)和.login(由C Shell使用)两个文件是.bash_profile的同义词,目的是为了兼容其它Shell。在Debian中使用.profile文件代 替.bash_profile文件。

.bashrc

文件会在bash shell调用另一个bash shell时读取,也就是在shell中再键入bash命令启动一个新shell时就会去读该文件。这样可有效分离登录和子shell所需的环境。但一般 来说都会在.bash_profile里调用.bashrc脚本以便统一配置用户环境。

.bash_logout

在退出shell时被读取。所以我们可把一些清理工作的命令放到这文件中。

在 /etc目录的bash.bashrc和profile是系统级(全局)的配置文件,当在用户主目录下找不到.bash_profile 和.bashrc\时,就会读取这两个文件。.bash_history是bash shell的历史记录文件,里面记录了你在bash shell中输入的所有命令。可通过HISSIZE环境变量设置在历史记录文件里保存记录的条数。alias l = ‘ls -l’是设置别名的语句,把它放在这些配置文档中就可使我们能用简单的’l’命令,代替’ls -l’命令。

当我们修改了这些配置件后,可用 source .bash_profile / source .bashrc 命令使它修改内容马上生效。

 

非管理员推荐通过编辑用户目录下的 ~/.bashrc 来设定Bash

vim ~/.bashr         #编辑.bashr
source .bashr        #重载Bash,使配置生效

如果管理员希望配置全局性的一些设定,首先要确定你理解了上述3种SHELL启动方式含义,推荐创建文件 /etc/bash.global 文件,内容如下。

并确保用户可以加载到这一文件。简单的处理方法通过 /etc/profile 和 /etc/bash.bashrc调用

这是一个简单的设置,启用了目录彩色显示,修改了命令提示符的格式,改写了几个命令(rm,cp,mv)的默认选项

#---- (lee)
# /etc/bashrc.global: for all shells.
# Add this lines to /etc/profile & /etc/bash.bashrc
# # inlucde global setting, --(lee)
# if [ -f /etc/bash.global ]; then
#     . /etc/bash.global
# fi

#命令提示行
#Promote
PS1='-------------------------------------\n\u@\h:\w\$ '
#Color Promote
PS1='\[\033[01;32m\]       -------------------------------------------------------\[\033[00m\]\n${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

# You may uncomment the following lines if you want `ls' to be colorized:
export LS_OPTIONS='--color=auto'
eval "`dircolors`"
alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -l'
alias l='ls $LS_OPTIONS -lA'
#
# Some more alias to avoid making mistakes:
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias vi='vim'

其它可以设置的选项

bash shell中的选项可控制shell的行为和功能,我们可以通过shopt命令来设置。使用set命令也可以,但它已被shopt替代,但为了向下兼 容,set命令还是可以使用的。使用不带参数的shopt命令可以列出当前shell中只能由shopt设置的选项,用shopt -o可列出可由set命令设置的选项。

这些设置你既可以随时通过命令行进行,也可以添加到上面的配置文件中

#下面是一些可由set命令基本的选项,默认是关闭的。
emacs                  进入emacs编辑模式
vi                     进入vi编辑模式
ignoreeof              不允许单独使用Ctrl_D退出的用法,要使用exit。与IGNOREEOF=10等价
noclobber              不允许重定向覆盖已存在文件
noglob                 不允许扩展文件名通配符
nounset                使用未定义的变量时给出错误

#下面是一些只能由shopt命令设置的选项
cdspell          自动改正cd命令参数中的小错误
hostcomplete     以@开头时,按tab键可进行主机名的自动完成
dotgblob         以点开始的文件名被包含在路径名扩展中
mailwarn         显示邮件警告信息

shopt命令的选项如下:

-p          显示可设置选项及当前取值
-s          设置每一选项为on
-u          设置每一选项为off
-q          不输出信息
-o

 

命令提示符

命令提示符就是在linux的命令行界面每行开始的的系统提示字符,类似 Debian:/var#

配置文件中export PS1=的内容是设定命令提示符(promote )的格式,可用的字符代码如下:

       \a     an ASCII bell character (07)
       \d     the  date  in  "Weekday  Month  Date" format
              (e.g., "Tue May 26")
       \e     an ASCII escape character (033)
       \h     the hostname up to the first `.'
       \H     the hostname
       \n     newline
       \r     carriage return
       \s     the name of the shell, the  basename  of  $0
              (the portion following the final slash)
       \t     the current time in 24-hour HH:MM:SS format
       \T     the current time in 12-hour HH:MM:SS format
       \@     the current time in 12-hour am/pm format
       \u     the username of the current user
       \v     the version of bash (e.g., 2.00)
       \V     the  release  of  bash, version + patchlevel
              (e.g., 2.00.0)
       \w     the current working directory
       \W     the basename of the current  working  direc-
              tory
       \!     the history number of this command
       \#     the command number of this command
       \$     if  the effective UID is 0, a #, otherwise a
              $

参考:


 

 

 

发表评论