Reputation: 20688
I have defined an alias like this.
[user@centos ~]$ alias echo='echo aliased echo says: '
[user@centos ~]$ echo hi
aliased echo says: hi
Why does alias substitution not take place when there is a backslash or part of the command is quoted as shown below? Can you explain it by citing the relevant sections from POSIX standard at http://pubs.opengroup.org/onlinepubs/9699919799/.
[user@centos ~]$ \echo hi
hi
[user@centos ~]$ ec\ho hi
hi
[user@centos ~]$ "echo" hi
hi
[user@centos ~]$ ec"ho" hi
hi
[user@centos ~]$ command echo hi
hi
[user@centos ~]$ command -v echo
alias echo='echo aliased echo says: '
Quoting the relevant section from http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
2.3.1 Alias Substitution
After a token has been delimited, but before applying the grammatical rules in Shell Grammar, a resulting word that is identified to be the command name word of a simple command shall be examined to determine whether it is an unquoted, valid alias name. However, reserved words in correct grammatical context shall not be candidates for alias substitution. A valid alias name (see XBD Alias Name) shall be one that has been defined by the alias utility and not subsequently undefined using unalias. Implementations also may provide predefined valid aliases that are in effect when the shell is invoked. To prevent infinite loops in recursive aliasing, if the shell is not currently processing an alias of the same name, the word shall be replaced by the value of the alias; otherwise, it shall not be replaced.
If the value of the alias replacing the word ends in a <blank>, the shell shall check the next command word for alias substitution; this process shall continue until a word is found that is not a valid alias or an alias value does not end in a <blank>.
When used as specified by this volume of POSIX.1-2008, alias definitions shall not be inherited by separate invocations of the shell or by the utility execution environments invoked by the shell; see Shell Execution Environment.
Is there anything in the above section that causes this?
Upvotes: 0
Views: 218
Reputation: 72707
Yes, there is something: the word unquoted in "... determine whether it is an unquoted, valid alias name." This sentence supresses alias substitution when any part of a command word is quoted.
There are three quoting mechanisms in the shell: backslash, single and double quotes.
Upvotes: 3