gpt4 book ai didi

shell - `alias sudo="sudo "` 如何工作?

转载 作者:行者123 更新时间:2023-12-04 02:01:36 26 4
gpt4 key购买 nike

寻找将当前用户的别名传递给 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 不关心两个命令之间有多少空格,这怎么会有什么影响呢?

手动添加空格时,我看不出有什么区别:
$ 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 允许传递别名。这适用于 zsh , bashsh ,所以它不是特定于 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

关于shell - `alias sudo="sudo "` 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37209913/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com