Bash manual says
The first word of the replacement text is tested for aliases, but a
word that is identical to an alias being expanded is not expanded a
second time. This means that one may aliaslstols -F, for
instance, and Bash does not try to recursively expand the replacement
text.
I’m trying to figure out which alias follows “identical to” in the quote,
- any alias being expanded in the same sequence of alias expansion recursions, or
- the alias whose expansion was first started, or
- the alias whose expansion was last started.
So I create an example
$ alias a1=a2; $ alias a2=a3; $ alias a3=a4;
and want to check the alias expansion result of a1, in the following cases
$ alias a4=a1;
or
$ alias a4=a2;
or
$ alias a4=a3;
How can I check the alias expansion result of a1, possibly by performing alias expansion on a1 without letting the shell going further than alias expansion?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
What the manual says is that the shell will avoid any loop that may be caused by recursion of alias expansion.
With your example (a1=a2=a3=a4), if you execute alias a4=a1 you are creating a loop. Then, as soon as you will execute a1 (resp. a2, a3, a4), once the shell loops back to a1 (resp. a2, a3, a4) it will search for a command named a1 (resp. a2, a3, a4) that is NOT an alias (since that would create a never-ending loop).
Example:
$ a1() { echo Phew, I got out of the loop; }
$ alias a1='echo "(a1)"; a2' a2='echo "(a2)"; a3'
$ alias a3='echo "(a3)"; a4' a4='echo "(a4)"; a1'
$ a1
(a1)
(a2)
(a3)
(a4)
Phew, I got out of the loop
$ a2 # Command a2 does not exist anywhere
(a2)
(a3)
(a4)
(a1)
a2: command not found
Method 2
:> alias e="echo " :> alias text=foo :> e text foo
Technically the shell does go further than alias expansion but nonetheless I think this is what you meant.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0