什么!!:2在Bash意味着什么?

时间:2015-12-23 20:41:11

标签: bash

我正在观看this视频教程,在1:40左右,作者发出以下命令。它在最近创建的文件夹下创建了两个文件。

touch !!:2/{package.json,index.js}

有人可以解释!!:2的含义吗?为什么它没有在所有文件夹下创建这两个文件?

1 个答案:

答案 0 :(得分:7)

在bash中,您可以使用!!重复上一个命令行。典型用法是:

do some command that needs root permissions
# this fails, so:
sudo !!

将扩展为:

sudo do some command that needs root permissions

表达式!!:2表示:从前一个命令行获取第三个(从0开始的索引)字。

举例说明:

$ echo these are some words in a sentence
these are some words in a sentence
$ echo !!:2
echo are
are

注意这非常容易出错,尤其是基于0的索引