`alias sudo="sudo "` 是如何工作的?

How does `alias sudo="sudo "` work?

寻找将当前用户的别名传递给 sudo 命令的方法,我发现了以下 on ArchWiki:

Passing aliases

If you use a lot of aliases, you might have noticed that they do not carry over to the root account when using sudo. However, there is an easy way to make them work. Simply add the following to your ~/.bashrc or /etc/bash.bashrc:

alias sudo='sudo '

我不明白为什么会这样。如果 shell 不关心两个命令之间有多少 space,这有什么影响?

手动添加 space 时,我看不出有什么区别:

$ alias e=echo
$ sudo e foo
sudo: e: command not found
$ sudo  e foo              # Manual space addition
sudo: e: command not found # Fail
$ alias sudo="sudo "       # Now with alias
$ sudo e foo
foo                        # Succeeds, but why?

明显将 sudo 别名为 sudo + space 以某种方式允许传递别名。这适用于 zshbashsh,因此它不是 shell 特定的行为。

这个别名是如何工作的?

正在查看别名的手册页:

A trailing space in VALUE causes the next word to be checked for alias substitution when the alias is expanded.

来源:http://www.linuxcommand.org/lc3_man_pages/aliash.html

我们从团队的最佳实践中得到了什么:

alias sodu='sudo'
alias sodo='sudo'
alias sdoo='sudo'
alias sudu='sudo'
alias sduo='sudo'

只需添加一个您认为对自己有必要的别名