感叹号在Linux bash中使用技巧

Posted gavin11

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了感叹号在Linux bash中使用技巧相关的知识,希望对你有一定的参考价值。

1. 重复执行上一条指令  !!

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

2. 重复执行上一条以a为首的指令 !a

[[email protected] 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
[[email protected] 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

[[email protected] 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
[[email protected] 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. 表示获得上一条命令中的最后一项内容  !$

[[email protected] 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
[[email protected] 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. 表示获得上一条命令中的第一个参数  !^

[[email protected] python]# 
[[email protected] 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
[[email protected] 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.上一命令除了最后一个参数   !:-

[[email protected] 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
[[email protected] python]# !:-
ls -l /etc/passwd
-rw-r--r-- 1 root root 1159 Jan 30 17:23 /etc/passwd
[[email protected] python]#

8.上一条命令中的所有参数   !*

[[email protected] python]# 
[[email protected] python]# ls /etc/passwd /home/python/1.py 
/etc/passwd  /home/python/1.py
[[email protected] 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
[[email protected] 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中使用技巧的主要内容,如果未能解决你的问题,请参考以下文章

linux中如何通过echo输出!(叹号)? -bash: !": event not found

-bash: /usr/bin/ls: /lib64/ld-linux-x86-64.so.2: bad ELF interpreter: No such file or directory(代码片段

感叹号:bash 的历史扩展功能

Linux bash基础特性二

sh Bash的代码片段

谢烟客---------Linux之bash脚本编程---if补充和for循环