几个节省时间的 bash 小技巧

57 阅读1分钟

使用 !*

使用 !* 将取得前一行(即除第一个单词外的整行)上所有参数的值

可以快速执行相同参数的命令


$ rm /var/log/httpd/access.log /var/log/httpd/error.log
$ touch !*
touch /var/log/httpd/access.log /var/log/httpd/error.log

使用 !$

使用 !$ 将获取到最后一个命令的最后一个参数

$ mv list.txt items.txt
$ vim !$
vim items.txt
$ cp !$ shopping.txt
cp items.txt shopping.txt

使用 !!

使用 !! 可以执行上一行命令

$ pwd
/etc
$ !!
pwd
/etc

如果你不想直接执行上一条命令,可以在末尾添加 :p 来调用,这样就是只显示不调用 。

$ rm -r temp/
$ mkdir temp
$ touch temp/test
$ !!
touch temp/test
$ history | tail -4
  179  rm -r temp/
  180  mkdir temp
  181  touch temp/test
  182  touch temp/test
  183  history | tail -5
$ !179:p
rm -r temp
$ !180
touch temp/test