linux环境变量学习

一、设置alias别名为全局运行环境

终端A操作:

[root@AbelTest ~]# which ls  //查看ls位置alias ls='ls --color=auto'        /usr/bin/ls
[root@AbelTest ~]# alias    //查看系统别名alias cp='cp -i'alias l.='ls -d .* --color=auto'alias ll='ls -l --color=auto'alias ls='ls --color=auto'alias mv='mv -i'alias rm='rm -i'alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@AbelTest ~]# alias a='ls -a'  //定义ls -a别名为a 适用当前环境[root@AbelTest ~]# a.                .bashrc.swp  .gconfd             .local            模板..               .cache       .gnome2             .nautilus         视频\                .config      .gtk-bookmarks      .pulse            图片anaconda-ks.cfg  .cshrc       .gvfs               .pulse-cookie     文档.bash_history    .dbus        .ICEauthority       .ssh              下载.bash_logout     .dmrc        .imsettings.log     .tcshrc           音乐.bash_profile    .esd_auth    install.log         .xsession-errors  桌面.bashrc          .gconf       install.log.syslog  公共的终端B操作:[root@AbelTest ~]# a             //找不到a命令-bash: a: command not found说明终端Aalias别名a的设置只适用于终端A的当前操作环境
终端A操作:定义ls -a别名为a 适用全局环境[root@AbelTest ~]# vi .bashrc    //编辑.bashrc文件# .bashrc# User specific aliases and functionsalias rm='rm -i'alias cp='cp -i'alias mv='mv -i'alias a='ls -a'               //增加alias a='ls -a'# Source global definitionsif [ -f /etc/bashrc ]; then        . /etc/bashrcfialias s='ls -alt'按Esc :wq 退出保存
打开终端C操作:[root@AbelTest ~]# a        //a命令执行.                .bashrc.swp  .gconfd             .local            模板..               .cache       .gnome2             .nautilus         视频\                .config      .gtk-bookmarks      .pulse            图片anaconda-ks.cfg  .cshrc       .gvfs               .pulse-cookie     文档.bash_history    .dbus        .ICEauthority       .ssh              下载.bash_logout     .dmrc        .imsettings.log     .tcshrc           音乐.bash_profile    .esd_auth    install.log         .xsession-errors  桌面.bashrc          .gconf       install.log.syslog  公共的说明全局运行环境设置成功

二、把文件设置成全局运行环境(类似windows中cmd命令的运行)

举例:install.log

[root@AbelTest ~]# echo $PATH      //显示当前PATH环境变量/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@AbelTest ~]# cp install.log /usr/bin        //把install.log拷贝到/usr/bin文件夹下[root@AbelTest ~]# chmod +x /usr/bin/install.log  //添加install.log运行权限[root@AbelTest ~]# install.log                    //执行成功/usr/bin/install.log: line 1: 安装: command not found/usr/bin/install.log: line 2: warning:: command not found/usr/bin/install.log: line 3: 安装: command not found/usr/bin/install.log: line 4: 安装: command not found

三、改变文件位置,并设置全局环境正常运行

举例:ls

终端A操作:

[root@AbelTest ~]# which ls                 //查看ls文件位置alias ls='ls --color=auto'         /usr/bin/ls                         //ls文件位置为/usr/bin/[root@AbelTest ~]# mv /usr/bin/ls /tmp/ls   //移动ls到/tmp/下[root@AbelTest ~]# ls                       //找不到命令-bash: ls: command not found[root@AbelTest ~]# which ls                 alias ls='ls --color=auto'                                           //找不到ls[root@AbelTest ~]# PATH=$PATH:/tmp/        //指定PATH变量路径[root@AbelTest ~]# which ls                alias ls='ls --color=auto'        /tmp/ls                            //ls文件位置为/tmp/[root@AbelTest ~]# ls                      //命令执行成功    \                install.log         公共的  视频  文档  音乐anaconda-ks.cfg  install.log.syslog  模板    图片  下载  桌面

终端B操作:

[root@AbelTest ~]# ls                  //找不到ls-bash: /usr/bin/ls: 没有那个文件或目录

说明以上ls的位置更改后执行只限于终端A的当前操作环境

终端A操作:

设置ls运行为全局环境

[root@AbelTest ~]# vi /etc/profile# By default, we want umask to get set. This sets it for login shell# Current threshold for system reserved uid/gids is 200# You could check uidgid reservation validity in# /usr/share/doc/setup-*/uidgid fileif [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then    umask 002else    umask 022fifor i in /etc/profile.d/*.sh ; do    if [ -r "$i" ]; then        if [ "${-#*i}" != "$-" ]; then            . "$i"        else            . "$i" >/dev/null 2>&1        fi    fidoneunset iunset -f pathmungePATH=$PATH:/tmp/          //添加/tmp/路径按Esc  :wq 退出保存

打开终端C操作:

[root@AbelTest ~]# ls                //执行成功\                install.log         公共的  视频  文档  音乐anaconda-ks.cfg  install.log.syslog  模板    图片  下载  桌面说明ls运行为全局环境。