linux環境變量

linux環境變量學習linux

1、設置alias別名爲全局運行環境shell

終端A操做:windows

[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 functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias a='ls -a'               //增長alias a='ls -a'
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
alias 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  公共的

說明全局運行環境設置成功

2、把文件設置成全局運行環境(相似windows中cmd命令的運行)bash

舉例:install.logcookie

[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

3、改變文件位置,並設置全局環境正常運行session

舉例:lsssh

終端A操做:ide

[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的當前操做環境ui

終端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 file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
fi

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
PATH=$PATH:/tmp/          //添加/tmp/路徑

按Esc  :wq 退出保存

打開終端C操做:

[root@AbelTest ~]# ls                //執行成功
\                install.log         公共的  視頻  文檔  音樂
anaconda-ks.cfg  install.log.syslog  模板    圖片  下載  桌面

說明ls運行爲全局環境。
相關文章
相關標籤/搜索