7.Shell
本章同步視頻:https://edu.51cto.com/sd/e4874
7.3 命令別名與歷史命令
7.3.1 命令別名 alias, unalias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]# alias ctmp="cd /tmp"
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost tmp]# unalias ctmp
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]# vim ~/.bashrc
bash: ctmp: command not found... #未生效
[root@localhost ~]# source .bashrc #執行配置文件
[root@localhost tmp]# su - calf #測試其他用戶
Last login: Tue Apr 14 05:04:31 EDT 2020 on pts/0
bash: ctmp: command not found... #無效
[root@localhost tmp]# vim /etc/bashrc #在最後加上一行
[root@localhost tmp]# source /etc/bashrc #執行配置文件
[root@localhost tmp]# su - calf #切換用戶
Last login: Tue Apr 14 05:04:48 EDT 2020 on pts/0
7.3.2 歷史命令history
[dmtsai@study ~]$ history [-c]
[dmtsai@study ~]$ history [-raw] histfiles
-c :將目前的 shell 中的所有 history 內容全部消除
-a :將目前新增的 history 指令新增入histfiles中,若沒有加histfiles,
-r :將histfiles的內容讀到目前這個 shell 的 history 記憶中;
-w :將目前的 history 記憶內容寫入histfiles中!
[root@localhost tmp]# history 5
command :由最近的指令向前搜尋『指令串開頭爲 command』的那個指令,並執行;
!! :就是執行上一個指令(相當於按↑按鍵後,按 Enter)
[root@localhost ~]# !478 #執行第478條指令
[root@localhost ~]# !echo #執行最近的以echo開頭的指令
[root@localhost ~]# !! #執行上一條指令
[root@localhost ~]# echo $HISTSIZE
[root@localhost ~]# export HISTSIZE=800
[root@localhost ~]# echo $HISTSIZE
[root@localhost ~]# vim /etc/profile
[root@localhost ~]# source /etc/profile
[root@localhost ~]# cat ~/.bash_history
#注:此時可能看不到最近使用的命令,因爲history是在用戶退出時寫入數據,如果需要提前寫入數據,可以執行以下代碼:
[root@localhost ~]# history -w
[root@localhost ~]# vim /etc/profile
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S `whoami` "
[root@localhost ~]# source /etc/profile
7 2020-04-09 09:43:57 root env
8 2020-04-09 09:44:01 root set
9 2020-04-09 09:57:33 root read name