码迷,mamicode.com
首页 > 系统相关 > 详细

感叹号在Linux bash中使用技巧

时间:2019-01-31 13:10:01      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:arc   passwd   style   arch   span   class   include   查看   一个   

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中使用技巧

标签:arc   passwd   style   arch   span   class   include   查看   一个   

原文地址:https://www.cnblogs.com/gavin11/p/10341190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!