This question is often asked on the Internet: Why does the alias I write not work under sudo?
$ Alias ' ll=ls-l ' $ sudo ll A-private-dir Sudo:ll:command not found |
Why is that? Because under normal circumstances, alias only appears in the position of the command name of a simple command (most of the time the position of the first word) will take effect, the command with the sudo prefix, it is clear that the command name is SUDO,LL will not be expanded.
The solution is magical, which is to set sudo itself as an alias:
$ Alias ' Sudo=sudo ' $ sudo ll A-private-dir -rw-r--r--1 root root ... ... |
Note the space behind the sudo on the right, which is the key. What is the principle? There are written in the Bash document:
If the last character of the alias value was a blank , then the next command word following the alias was also checked for AL IAS expansion.
If the last character of an alias's expanded value is a white space (space, tab, line break), then the word immediately following it will be expanded if it is alias. Let me give you an example:
$ Alias ' Echo=echo foo ' $ echo Echo Echo # Chained unfold, three echo all expanded Foo echo foo echo foo
|
This feature seems to be specifically tailored for sudo, because there is no other usage scenario.
I would like to know the source of this feature, which Shell was implemented first. Then checked: SH does not have the alias function, CSH First has the alias function, but it does not have this feature; Ksh88 had this feature when he copied the alias from CSH, and Bash had it for 87 years, so it was probably the bash that was learned from Ksh. feature, which is probably the one that David Korn invented.
Alias ending with a blank character