4 ms·
I typically just give up on weird corners of shell when I find an working version of the same. For instance -- why would you use "alias" when you can make a fu
by cduzz 2y ago
I typically just give up on weird corners of shell when I find an working version of the same.
For instance -- why would you use "alias" when you can make a function? The syntax is a little weird with functions, but it's a lot more clear what's going on.
The same goes for "test" vs the seeming magic of [ where it seems like [ is language syntax (it's a single character!) when in fact ... it's just another executable that communicates with logic evaluation like anything else (like grep or false).
- alganet 2y agoThere 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.