ksh 中的最后一个命令

Xod*_*rap 5 ksh command-history

在 bash 中,我可以!!用来指示最后一个命令。在 ksh 中,我认为您可以r用来做类似的事情,但它似乎以我的常见模式之一失败:键入sudo [last command].

> cp foo /bar
cp: permission denied
> r c       # If I do this it will repeat my cp call
> sudo r c  # when I do this, it tells me that "r" is not a command
Run Code Online (Sandbox Code Playgroud)

如何使用历史记录快速输入“sudo [last command]”?

小智 1

我能做到这一点的最好方法是创建一个自动加载函数,例如sudo_r

function sudo_r {
  typeset comm=$(hist -nl ${@:- -1 -1} | sed 1q);
  print -- $comm;
  sudo $comm;
}
Run Code Online (Sandbox Code Playgroud)

然后(比如说)“alias ss=sudo_r”。完成此操作后,您可以键入ss cp以重复以 开头的最后一个命令cp,但以 为前缀sudo。或者如果您输入ss Return,它将是sudo上一个命令。