bash 用“sudo”重复上一个命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17245614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:52:15  来源:igfitidea点击:

Repeat last command with "sudo"

bash

提问by Nemo

I often forget to run commands with sudo. I'm looking for a way to make a bash function (or alias) for repeating the last command with sudo. Something like:

我经常忘记用sudo. 我正在寻找一种方法来制作 bash 函数(或别名),以使用 .bashrc 重复最后一个命令sudo。就像是:

S() {
    sudo $(history 1)
}

Any ideas?

有任何想法吗?

回答by ruakh

回答by jm666

Not enough?

不够?

sudo !!

if you want the Ssimply put:

如果你想要S简单地说:

alias S=sudo

and use it

并使用它

S !!

the !!mean the last command

!!均值last command

回答by Aaron Franke

Use alias redo='sudo $(history -p !!)'. This is the only thing that I have found that works with aliases. The other answers do not work in aliases for some reason, having tried them myself, even though they do work when directly running them.

使用alias redo='sudo $(history -p !!)'. 这是我发现的唯一可以使用别名的东西。由于某些原因,其他答案在别名中不起作用,我自己尝试过,即使它们在直接运行时确实有效。

回答by Koterpillar

!!can be used to reference the last command. So:

!!可用于引用最后一条命令。所以:

sudo !!

回答by DiskJunky

Adding and expanding upon aaron franke's response (which is correct but lacking a why) such that simply typing redoworks after configuring the alias alias redo='sudo $(history -p !!);

添加和扩展aaron franke的响应(这是正确的但缺乏原因),以便redo在配置别名后只需键入即可alias redo='sudo $(history -p !!)

This is the only thing I found that works with aliases

这是我发现的唯一可以使用别名的东西

There are a few things going on that warrant explanation.

有一些事情发生在那个授权解释上。

alias redo='sudo !!'

doesn't work as it doesn't execute the sudoportion, though it does resolve the !!.

不起作用,因为它不执行该sudo部分,尽管它确实解决了!!.

To get the sudo !!to resolve correctly, a shell resolution directive has to execute and concatenate along with sudo. So we do;

sudo !!正确解析 ,shell 解析指令必须执行并与sudo. 我们这样做;

alias redo='sudo $(history -p !!)'

By specifying the right side of the alias to $(history -p !!), what this does is say to the shell;

通过将别名的右侧指定为$(history -p !!),这就是对 shell 说的;

  1. redois an alias, assess the right side of the =
  2. sudoremains as is and it concatenated to...
  3. $()is a shell directive, to execute the contents within the current execution process (as oppose to a sub-shell ${}, which spawns a different process to execute within)
  4. Resolve history -p !!
  5. The !!gets expanded to history -p <lastCommandHere>as a result of step 4.
  6. The -ppart says to historyto only print the results, not execute
  7. The sudo, executes the remaining (now printed out on the same command line), with elevated privileges, assuming password was entered correctly
  1. redo是别名,评估右侧 =
  2. sudo保持原样,它连接到......
  3. $()是一个 shell 指令,用于执行当前执行进程中的内容(与 sub-shell 相对${},它产生一个不同的进程在其中执行)
  4. 解决 history -p !!
  5. !!被扩展到history -p <lastCommandHere>作为步骤4的结果。
  6. -p部分说要history只打印结果,不执行
  7. sudo,执行剩余的(现在打印输出的同一命令行上),以高特权,假定密码被正确输入

This ultimately means that the command history -p !!effectively writes out the resolution rather than executing after the sudo.

这最终意味着该命令history -p !!有效地写出分辨率而不是在sudo.

NOTE: The 'is significant. If you use ", the !!gets interpolated twice and to achieve our purpose we need to very carefully control the resolution steps; single/double quotes in bash

注意'很重要。如果您使用",则会!!被插值两次,为了达到我们的目的,我们需要非常小心地控制解析步骤;bash中的单引号/双引号

PS

聚苯乙烯

If you're using zsh+ oh-my-zsh, there is a sudoalias setup for _such that you can do;

如果您使用zsh+ oh-my-zsh,则可以sudo设置别名_

_ !!

Which will rerun the last command as sudo. However, if you're configuring the alias in ~/.zshrc, the alias redo='sudo $(history -p !!)will not work as is from the zsh config as the resolution steps are different.

这将重新运行最后一个命令作为sudo. 但是,如果您在 中配置别名~/.zshrcalias redo='sudo $(history -p !!)由于解析步骤不同,zsh 配置将无法正常工作。

That said, you can put the alias into ~/.bashrcinstead of ~/.zshrcand have the same result when executing the alias redofrom zsh (assuming you're sub-shelling zsh from bash, though this is admittedly a bit of a kludge - though a functional one).

也就是说,当从 zsh执行别名时,您可以将别名放入~/.bashrc而不是~/.zshrc并获得相同的结果redo(假设您是从 bash 对 zsh 进行子壳,尽管这确实有点麻烦 - 尽管是功能性的)。