Alias ending with a blank character

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.