Bash – Does one alias affect another alias

aliasbash

I don't have many aliases set up in my .bash_aliases file just yet. Only recently have I discovered how useful they can be.

I can see myself getting quite hooked on aliases so before the file gets too unwieldy I'd like to clarify something by asking a very simple question. Does one alias affect another, or does the alias always revert back to the original command?

An example may make this more clear:

I have ls aliased to ls -lF. Let's say I'd like to also alias d to ls -l | grep -E "^d": does d now use ls -lF in place of ls? If so, is it a matter of order in the .bash_aliases configuration file?

man alias didn't enlighten me.

Best Answer

From Aliases (section 6.6 of the Bash Manual):

The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias.

This happens when you use the alias, not when you define it. Here's an example:

$ alias a1='a2 hello'
$ alias a2='echo'
$ a1
hello
$ unalias a2
$ a1
bash: a2: command not found
Related Question