shell介紹,命令歷史,命令補全和別名,通配符 ,輸入輸出重定向
阿新 • • 發佈:2018-04-21
shell介紹命令歷史命令補全和別名shell介紹
-
shell是一個命令解釋器,提供用戶和機器之間的交互
-
支持特定語法,比如邏輯判斷,循環
-
每個用戶都可以有自己特定的shell
-
CentOS7默認shell為bash(Bourne Agin Shell)
-
還有zsh,ksh等
命令歷史 history
[root@localhost ~]# ls /root/.bash_history
/root/.bash_history
[root@localhost ~]#
-
最大存1000條。
[root@localhost ~]# echo $HISTSIZE
1000
[root@localhost ~]#
-
變量HISTSIZE 在/etc/profile中修改
vi /etc/profile 找到 HOSTNAME=`/usr/bin/hostname 2>/dev/null` HISTSIZE=1000 if [ "$HISTCONTROL" = "ignorespace" ] ; then 如果覺得1000條不夠可以改成5000
更改之後必須退出一下終端重新進或者source /etc/profile 才會更改
[root@localhost ~]# echo $HISTSIZE 1000 [root@localhost ~]# source /etc/profile [root@localhost ~]# echo $HISTSIZE 5000 [root@localhost ~]#
-
變量重新賦值
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
[root@localhost ~]# history 1 ls 2 ls /etc/passwd 3 cat /etc/passwd 4 [root@localhost ~]# cat /etc/passwd 5 root:x:0:0:root:/root:/bin/bash 6 bin:x:1:1:bin:/bin:/sbin/nologin 7 daemon:x:2:2:daemon:/sbin:/sbin/nologin 8 adm:x:3:4:adm:/var/adm:/sbin/nologin 9 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
重新賦值之後
[root@localhost ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[root@localhost ~]# history
1 2017/08/30 19:20:18 ls
2 2017/08/30 19:20:18 ls /etc/passwd
3 2017/08/30 19:20:18 cat /etc/passwd
4 2017/08/30 19:20:18 [root@localhost ~]# cat /etc/passwd
5 2017/08/30 19:20:18 root:x:0:0:root:/root:/bin/bash
6 2017/08/30 19:20:18 bin:x:1:1:bin:/bin:/sbin/nologin
7 2017/08/30 19:20:18 daemon:x:2:2:daemon:/sbin:/sbin/nologin
8 2017/08/30 19:20:18 adm:x:3:4:adm:/var/adm:/sbin/nologin
這時候再打開一個新的終端,這個環境變量是不生效的。
如果想讓此環境變量全局生效,需要編輯 /etc/profile把HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "加到裏面
fi
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=5000
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
if [ "$HISTCONTROL" = "ignorespace" ] ; then
export HISTCONTROL=ignoreboth
source /etc/profile後生效
-
永久保存命令歷史
chattr +a ~/.bash_history
只能追加不能刪除
-
如果沒有正常退出,歷史命令保存不全
-
!!
表示運行最後一條歷史命令
-
!n
指定運行歷史行數命令
-
!命令
表示命令歷史裏面最後一次輸入這個命令的命令
命令補全及別名
-
tab鍵,敲一下,敲兩下
-
Centos7 支持參數補全,默認不可以,需要安裝yum install -y bash-completion 之後重啟下系統才能生效。
-
alias別名,給命令從新起個名字
[root@localhost ~]# alias restartnet=‘systemctl restart network.service‘
[root@localhost ~]# restartnet
[root@localhost ~]# alias
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 restartnet=‘systemctl restart network.service‘
alias rm=‘rm -i‘
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘
[root@localhost ~]#
alias 存在用戶家目錄下的 .bashrc文件裏。
還有一些存在 /etc/profile.d/下面
unalias 取消自定義別名
通配符
*
[root@localhost ~]# ls
111 1eer 1.txt 2.txt anaconda-ks.cfg erwe
[root@localhost ~]# ls *.txt
1.txt 2.txt
[root@localhost ~]#
[root@localhost ~]# ls 1*
1eer 1.txt
111:
[root@localhost ~]# ls *1*
1eer 1.txt
111:
[root@localhost ~]#
?
問號表示任意一個字符
[root@localhost ~]# ls ?.txt
1.txt 2.txt
[root@localhost ~]# ls *.txt
1.txt 2.txt bbcc.txt bb.txt
[root@localhost ~]#
[]
選擇範圍
[root@localhost ~]# ls [123].txt
1.txt 2.txt
[root@localhost ~]# ls [0-9].txt
1.txt 2.txt
[root@localhost ~]# ls [1].txt
1.txt
[root@localhost ~]# ls [2].txt
2.txt
[root@localhost ~]#
{}
也是選擇範圍,跟[]差不多,但是中間有逗號
[root@localhost ~]# ls {1,2}.txt
1.txt 2.txt
[root@localhost ~]#
輸入輸出重定向
>
把>前面的內容刪除掉重定向到後面內容去。
>>
把>>前面的內容追加到後面內容去。
2>
表示把運行錯誤的信息定向到一個文件裏
[root@localhost ~]# fddd
-bash: fddd: 未找到命令
[root@localhost ~]# fddd 2> 1.txt
[root@localhost ~]# cat 1.txt
-bash: fddd: 未找到命令
2>>
錯誤追加重定向
&>
錯誤和正確都有的輸出重定向
[root@localhost ~]# ls [12].txt asffsfs.txt &> 1.txt
[root@localhost ~]# cat 1.txt
ls: 無法訪問asffsfs.txt: 沒有那個文件或目錄
1.txt
2.txt
[root@localhost ~]#
<
輸入重定向
把<右邊的文件內容輸入到<左邊的命令裏面去
[root@localhost ~]# wc -l < 1.txt
3
[root@localhost ~]#
shell介紹,命令歷史,命令補全和別名,通配符 ,輸入輸出重定向