2 ms·
There are many reasons. 1. Aliases can work with the callee scope. alias MY_VAR_MODIFIER='local foo=bar' MY_VAR_MODIFIER () { local foo=bar; } Call
by alganet 2y ago
There are many reasons.
1. Aliases can work with the callee scope.
alias MY_VAR_MODIFIER='local foo=bar'
MY_VAR_MODIFIER () { local foo=bar; }
Calling the alias by the name will set the callee's variable foo, while calling the function does nothing (local foo is local to the function and never leaves scope).
It also works similarly for working with the set ($@). You can do `set --` stuff and it works on the scope of the callee.]
Aliases on most shells also don't need to be fully valid _before_ expansion. You can alias a compound command:
alias foreach='for EACH'
foreach in $MYVAR #perfectly valid for most shells.
Only ksh93 will complain about it, it requires aliases to be complete valid shell programs.
Finally, alias calls don't appear in xtrace (set -x). Only the final expansion will appear.
TL;DR aliases in scripts work a lot like macros.