使用“history”命令触发上次运行命令?


Answers:



6

正如伊格纳西奥所说,使用 !!,但如果你坚持:

$(history | sed '$d' | sed -n '$p' | cut -d ' ' -f 3-)

说明:

  • $() 执行命令输出
  • sed '$d' 删除最后一个命令,即 2001 $(history | sed '$d' | sed -n '$p' | cut -d ' ' -f 3-)
  • sed -n '$p' 仅打印最后一行,即 2000 yourpreviouscommand
  • cut -d ' ' -f 3- 删除前三个字段,用空格分隔,即 <space>2000<space>

我认为它可以更简洁和强大,但它现在有效。

编辑 :如果你想使用 history 代替 !! 因为你正在使用的shell没有 !!,你运气不好; history 是内置的shell,而不是程序。如果他们没有,简单的贝壳不太可能拥有它 !!。例如,破折号不会:

imgx64@home:~$ dash
$ history
dash: history: not found
$ 

1
深恶痛绝。非常好。
dmckee

3

在Bash, fc 命令可以做你想要的。

man bash

...
在第二种形式中,在每个实例之后重新执行命令                 pat替换为rep。与此一起使用的有用别名是 r="fc -s",这样打字 r cc 运行最后一个命令                 以。。。开始 cc 和打字 r 重新执行最后一个命令。
...

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.