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