1. 程式人生 > >感嘆號在Linux bash中使用技巧

感嘆號在Linux bash中使用技巧

har sha idl 找到 lin size ins fig 號碼

1. 重復執行上一條指令 !!

[root@iZ23t6nzr7dZ python]# ls /usr/local/
aegis  bin  etc  games  include  lib  lib64  libexec  python3  sbin  share  src
[root@iZ23t6nzr7dZ python]# !!
ls /usr/local/
aegis  bin  etc  games  include  lib  lib64  libexec  python3  sbin  share  src
[root@iZ23t6nzr7dZ python]#

2. 重復執行上一條以a為首的指令 !a

[root@iZ23t6nzr7dZ python]# history
  248  rpm -qf /usr/bin/pwd
  249  rpm -ql coreutils
  250  clear
  251  /usr/local/
  252  ls /usr/local/
  253  ls ls /usr/local/
  254  ls /usr/local/
  255  ls -al /usr/local/
  256  vim /etc/shadow
  257  ls
  258  vim /etc/passwd
  259  clear
  260  ls
  261  cp -rf ./* /tmp/
  262  ls -l /tmp/
  263  ls -l
  264  clear
  265  ls /usr/local/
  266  history
[root@iZ23t6nzr7dZ python]# !rpm
rpm -ql coreutils
/etc/DIR_COLORS
/etc/DIR_COLORS.256color
/etc/DIR_COLORS.lightbgcolor
/etc/profile.d/colorls.csh
/etc/profile.d/colorls.sh
/usr/bin/[
/usr/bin/arch
/usr/bin/base64

3. 重復執行上一條在history表中記錄號碼為number的指令 !number

[root@iZ23t6nzr7dZ python]#history
  259  clear
  260  ls
  261  cp -rf ./* /tmp/
  262  ls -l /tmp/
  263  ls -l
  264  clear
  265  ls /usr/local/
  266  history
  267  rpm -ql coreutils
  268  clear
  269  history
[root@iZ23t6nzr7dZ python]# !267
rpm -ql coreutils
/etc/DIR_COLORS
/etc/DIR_COLORS.256color
/etc/DIR_COLORS.lightbgcolor
/etc/profile.d/colorls.csh
/etc/profile.d/colorls.sh
/usr/bin/[
/usr/bin/arch

4.重復執行前第number條指令 !-number

5. 表示獲得上一條命令中的最後一項內容 !$

[root@iZ23t6nzr7dZ python]# ls -l /usr/local/python3/bin/ /etc/passwd
-rw-r--r-- 1 root root 1159 Jan 30 17:23 /etc/passwd

/usr/local/python3/bin/:
total 27340
lrwxrwxrwx 1 root root        8 Jan 30 11:11 2to3 -> 2to3-3.7
-rwxr-xr-x 1 root root      109 Jan 30 11:11 2to3-3.7
-rwxr-xr-x 1 root root      225 Jan 30 12:07 chardetect
-rwxr-xr-x 1 root root      250 Jan 30 11:11 easy_install-3.7
lrwxrwxrwx 1 root root        7 Jan 30 11:11 idle3 -> idle3.7
-rwxr-xr-x 1 root root      107 Jan 30 11:11 idle3.7
-rwxr-xr-x 1 root root      232 Jan 30 11:11 pip3
-rwxr-xr-x 1 root root      232 Jan 30 11:11 pip3.7
lrwxrwxrwx 1 root root        8 Jan 30 11:11 pydoc3 -> pydoc3.7
-rwxr-xr-x 1 root root       92 Jan 30 11:11 pydoc3.7
lrwxrwxrwx 1 root root        9 Jan 30 11:11 python3 -> python3.7
-rwxr-xr-x 2 root root 13978072 Jan 30 11:10 python3.7
lrwxrwxrwx 1 root root       17 Jan 30 11:11 python3.7-config -> python3.7m-config
-rwxr-xr-x 2 root root 13978072 Jan 30 11:10 python3.7m
-rwxr-xr-x 1 root root     3105 Jan 30 11:11 python3.7m-config
lrwxrwxrwx 1 root root       16 Jan 30 11:11 python3-config -> python3.7-config
lrwxrwxrwx 1 root root       10 Jan 30 11:11 pyvenv -> pyvenv-3.7
-rwxr-xr-x 1 root root      449 Jan 30 11:11 pyvenv-3.7
[root@iZ23t6nzr7dZ python]# cat !$
cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

6. 表示獲得上一條命令中的第一個參數 !^

[root@iZ23t6nzr7dZ python]# 
[root@iZ23t6nzr7dZ python]# ls -l /usr/local/python3/bin/ /etc/passwd
-rw-r--r-- 1 root root 1159 Jan 30 17:23 /etc/passwd

/usr/local/python3/bin/:
total 27340
lrwxrwxrwx 1 root root        8 Jan 30 11:11 2to3 -> 2to3-3.7
-rwxr-xr-x 1 root root      109 Jan 30 11:11 2to3-3.7
-rwxr-xr-x 1 root root      225 Jan 30 12:07 chardetect
-rwxr-xr-x 1 root root      250 Jan 30 11:11 easy_install-3.7
lrwxrwxrwx 1 root root        7 Jan 30 11:11 idle3 -> idle3.7
-rwxr-xr-x 1 root root      107 Jan 30 11:11 idle3.7
-rwxr-xr-x 1 root root      232 Jan 30 11:11 pip3
-rwxr-xr-x 1 root root      232 Jan 30 11:11 pip3.7
lrwxrwxrwx 1 root root        8 Jan 30 11:11 pydoc3 -> pydoc3.7
-rwxr-xr-x 1 root root       92 Jan 30 11:11 pydoc3.7
lrwxrwxrwx 1 root root        9 Jan 30 11:11 python3 -> python3.7
-rwxr-xr-x 2 root root 13978072 Jan 30 11:10 python3.7
lrwxrwxrwx 1 root root       17 Jan 30 11:11 python3.7-config -> python3.7m-config
-rwxr-xr-x 2 root root 13978072 Jan 30 11:10 python3.7m
-rwxr-xr-x 1 root root     3105 Jan 30 11:11 python3.7m-config
lrwxrwxrwx 1 root root       16 Jan 30 11:11 python3-config -> python3.7-config
lrwxrwxrwx 1 root root       10 Jan 30 11:11 pyvenv -> pyvenv-3.7
-rwxr-xr-x 1 root root      449 Jan 30 11:11 pyvenv-3.7
[root@iZ23t6nzr7dZ python]# ls !^
ls -l
total 92
-rw-r--r-- 1 root root   958 Jan 30 18:16 1.py
-rw-r--r-- 1 root root   998 Jan 30 21:01 2.py
-rw-r--r-- 1 root root    59 Jan 30 21:15 3.py
-rw-r--r-- 1 root root 59824 Jan 30 20:53 links.txt
-rw-r--r-- 1 root root 14956 Jan 30 20:59 wangping_links_2.txt
-rw-r--r-- 1 root root    32 Jan 30 20:59 wp_links_2.txt

7.上一命令除了最後一個參數 !:-

[root@iZ23t6nzr7dZ python]# ls -l  /etc/passwd /home/python/
-rw-r--r-- 1 root root 1159 Jan 30 17:23 /etc/passwd

/home/python/:
total 92
-rw-r--r-- 1 root root   958 Jan 30 18:16 1.py
-rw-r--r-- 1 root root   998 Jan 30 21:01 2.py
-rw-r--r-- 1 root root    59 Jan 30 21:15 3.py
-rw-r--r-- 1 root root 59824 Jan 30 20:53 links.txt
-rw-r--r-- 1 root root 14956 Jan 30 20:59 wangping_links_2.txt
-rw-r--r-- 1 root root    32 Jan 30 20:59 wp_links_2.txt
[root@iZ23t6nzr7dZ python]# !:-
ls -l /etc/passwd
-rw-r--r-- 1 root root 1159 Jan 30 17:23 /etc/passwd
[root@iZ23t6nzr7dZ python]#

8.上一條命令中的所有參數 !*

[root@iZ23t6nzr7dZ python]# 
[root@iZ23t6nzr7dZ python]# ls /etc/passwd /home/python/1.py 
/etc/passwd  /home/python/1.py
[root@iZ23t6nzr7dZ python]# ls -l !*
ls -l /etc/passwd /home/python/1.py
-rw-r--r-- 1 root root 1159 Jan 30 17:23 /etc/passwd
-rw-r--r-- 1 root root  958 Jan 30 18:16 /home/python/1.py
[root@iZ23t6nzr7dZ python]# 

9.使用上條命令指定的參數 ![命令名]:[參數號]

$ cp -rf dira dirb/ #將dira拷貝到dirb

$ ls -l !cp:2 #查看dira的內容

ls -l dira

total 0

-rw-rw-r-- 1 hyb hyb 0 Jan 20 16:18 barfile

10. 用Ctrl + r 組合鍵來進入歷史搜索模式在history表中查詢某條過往指令,找到需要重復執行的命令後,按回車鍵即可重復命令參數(即上一點中的第5條)

感嘆號在Linux bash中使用技巧