简体   繁体   English

`别名sudo =“sudo”`工作怎么样?

[英]How does `alias sudo=“sudo ”` work?

Looking at ways to pass current user's aliases to a sudo command, I found the following on ArchWiki : 看看将当前用户的别名传递给sudo命令的方法,我在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. 如果您使用了很多别名,您可能已经注意到在使用sudo时它们不会转移到root帐户。 However, there is an easy way to make them work. 但是,有一种简单的方法可以使它们工作。 Simply add the following to your ~/.bashrc or /etc/bash.bashrc : 只需将以下内容添加到~/.bashrc/etc/bash.bashrc

alias sudo='sudo '

I don't get why this works. 我不明白为什么会这样。 If the shell does not care how many spaces are between two commands, how can this have any effect? 如果shell不关心两个命令之间有多少空格,这怎么会有效?

When manually adding a space, I see no difference: 手动添加空格时,我认为没有区别:

$ 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?

Visibly aliasing sudo to sudo + space somehow allows passing aliases. 可见别名sudo到sudo + space以某种方式允许传递别名。 This works on zsh , bash and sh , so it is not a shell-specific behavior. 这适用于zshbashsh ,因此它不是特定于shell的行为。

How does this alias work? 这个别名如何运作?

Looking at the man page for alias: 查看别名的手册页:

A trailing space in VALUE causes the next word to be checked for alias substitution when the alias is expanded. VALUE中的尾随空格会在扩展别名时检查下一个单词的别名替换。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM