1. 命令別名
定義clear別名爲cls [root@localhost ~]# alias cls=clear 打印當前已生效別名 [root@localhost ~]# alias alias cls='clear' alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' 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' 取消別名定義cls [root@localhost ~]# unalias cls 執行命令本身而不是別名 [root@localhost ~]# \ls anaconda-ks.cfg bd_logo1.png
2. 命令歷史
查看所有有記錄的歷史命令 [root@localhost ~]# history 查看10條最近記錄的歷史命令 [root@localhost ~]# history 10 執行history列表中第10號命令 [root@localhost ~]# !10 執行history列表中最近一條以echo開頭的命令 [root@localhost ~]# !echo 刪除history列表中第10號命令 [root@localhost ~]# history -d 10 將當前緩衝的歷史行追加到歷史文件中 [root@localhost ~]# history -a 清空history列表 [root@localhost ~]# history -c 從歷史文件中讀取所有未被讀取的行 [root@localhost ~]# history -n 讀取歷史文件並將內容追加到歷史列表中 [root@localhost ~]# history -r
命令歷史相關的環境變量
[root@localhost ~]# echo $HISTSIZE 1000 [root@localhost ~]# echo $HISTFILESIZE 1000 [root@localhost ~]# echo $HISTFILE /root/.bash_history [root@localhost ~]# echo $HISTCMD 733 [root@localhost ~]# echo $HISTCONTROL ignoredups
$HISTCONTROL有三種取值分別表示
ignoredups:忽略重複的命令;重複是指連續且相同的命令; ignorespace:以空白字符開頭的命令不記入歷史; ignoreboth:上述兩者同時生效;
3. 快捷鍵
Ctrl+a:跳至命令行首; Ctrl+e:跳至命令行尾; Ctrl+k:刪除光標所在處至尾部的內容; Ctrl+u:刪除行首至光標所在處的內容; Ctrl+w:刪除行尾一個單詞(非英語單詞,而是以空格分隔開的一段連續字符串);
4. 自動補全
略
5. 命令行展開
[root@localhost ~]# mkdir -pv /tmp/rootfs/{bin,sbin,lib,lib64,tmp,usr/{,local}/{bin,sbin},var/{log/httpd,spool}}
6. 命令執行狀態與執行結果
[root@localhost ~]# echo $(date +%H:%M:%S) 22:13:06 [root@localhost ~]# echo `date +%H:%M:%S` 22:13:24 [root@localhost ~]# echo $? 0
7. 強引用與弱引用
[root@localhost ~]# echo '$SHELL' $SHELL [root@localhost ~]# echo "$SHELL" /bin/bash
8. 文件名通配
[root@localhost ~]# ls -l /etc/[ekm]*[[:digit:]]*.conf -rw-r--r--. 1 root root 112 Mar 6 2015 /etc/e2fsck.conf -rw-r--r--. 1 root root 495 Mar 6 2015 /etc/krb5.conf -rw-r--r--. 1 root root 936 Mar 6 2015 /etc/mke2fs.conf
常用的通配符有以下幾個
*:匹配任意長度的任意字符; ?:匹配任意單個字符; [ ]:匹配指定集合內的任意單個字符; [^ ]:匹配指定集合外的任意單個字符;
更復雜的用法參照
[root@localhost tmp]# man 7 glob
注意:glob與正則表達式的區別。
9. 變量
bash是弱類型編程語言,bash中變量統統默認爲字符型數據,且無需事先聲明而直接使用。
bash變量有三種類型
本地變量, 作用域爲當前shell進程,不包含其子進程;
環境變量, 作用域爲當前shell進程及其子進程;
局部變量, 作用域爲定義此變量的函數。
環境變量
Linux是一個多用戶的操作系統。每個用戶登錄系統後,都會有一個專用的運行環境。通常每個用戶默認的環境都是相同的,這個默認環境實際上就是一組環境變量的定義。
查看當前用戶登錄bash的環境變量
[root@localhost ~]# env [root@localhost ~]# printenv 或者 [root@localhost ~]# export [root@localhost ~]# declare -x
只讀變量
[root@localhost ~]# declare -r myvar="how are you?" [root@localhost ~]# readonly myvar="how are you?"
查看當前bash進程中的只讀變量
[root@localhost ~]# readonly declare -r BASHOPTS="checkwinsize:cmdhist:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath" declare -ir BASHPID declare -r BASH_COMPLETION_COMPAT_DIR="/etc/bash_completion.d" declare -ar BASH_VERSINFO='([0]="4" [1]="2" [2]="46" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")' declare -ir EUID="0" declare -ir PPID="2814" declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor" declare -ir UID="0" declare -r myvar="how are you?"
只讀變量不支持重新賦值,也不支持unset操作, 只能退出當前bash進程,該變量纔會消失。
10. 重定向與管道
標準輸入(stdin) 文件描述符爲0 標準輸出(stdout) 文件描述符爲1 標準錯誤輸出(stderr) 文件描述符爲2
標準輸出重定向
[root@localhost tmp]# echo "how are you?" > demo #覆蓋 [root@localhost tmp]# echo "how old are you?" >> demo #追加
標準錯誤輸出重定向
[root@localhost tmp]# echoooo "how are you?" 2> demo #覆蓋 [root@localhost tmp]# echoooo "how old are you?" 2>> demo #追加
合併標準輸出與標準錯誤輸出
[root@localhost tmp]# echo "how are you?" &> demo #覆蓋 [root@localhost tmp]# echo "how old are you?" &>> demo #追加 或者 [root@localhost tmp]# echo "how are you?" > demo 2>&1 #覆蓋 [root@localhost tmp]# echo "how old are you?" &>> demo 2>&1 #追加
標準輸入重定向
[root@localhost ~]# tr [a-z] [A-Z] < /etc/issue \S KERNEL \R ON AN \M MAGE EDUCATION LEARNING SERVICES HTTP://WWW.MAGEDU.COM
Here Document
cat << EOF > /tmp/issue.doc \S Kernel \r on an \m Mage Education Learning Services http://www.magedu.com EOF
管道
[root@localhost ~]# cat /etc/issue | tee /tmp/$(date +%Y%m%d%H%M%S)
11. 環境配置
bash的環境配置文件根據功能來分有三類:
profile類:爲shell進程實現初始化配置。主要是運行初始化命令和腳本,並設置環境變量。
bashrc類:爲shell進程實現啓動配置。主要是定義umask和命令別名,並設置本地變量。
logout類:爲shell進程提供終止進程前的清理工作,主要是清理變量和臨時文件。
logout類的環境配置文件一般不會去做自定義修改,因此後面不討論它。
bash的環境配置文件根據作用範圍來分有兩類:
全局:對所有用戶都生效。/etc/profile, /etc/profile.d/*.sh, /etc/bashrc
用戶:僅對某個用戶生效。~/.bash_profile, ~/.bash_rc
登錄式shell與非登錄式shell啓動時加載的配置文件數量和順序不同。
登錄式shell:賬戶登錄的shell。如通過終端登錄的shell,“su -l USERNAME”。
/etc/profile --> /etc/profile.d/*.sh --> ~/.bash_profile --> ~/.bashrc --> /etc/bashrc
非登錄式shell:沒有通過賬戶登錄的shell。如通過桌面環境打開的命令行接口,“su USERNAME”
~/.bashrc --> /etc/bashrc --> /etc/profile.d/*.sh
注意:
全局環境配置文件只有管理員有權限修改。
只有加載了的配置文件中的環境配置纔會生效。
當不同的配置文件中設置了相同的變量但值不同,後加載的會覆蓋先前的配置。