5 ms·
FWIW the title says "functions", but it apparently uses aliases. Shell functions have all the same functionality as aliases and they'll catch more syntax error
by chubot 5y ago
FWIW the title says "functions", but it apparently uses aliases.
Shell functions have all the same functionality as aliases and they'll catch more syntax errors upon "source myscript". Example:
ls() {
command ls --color=auto "$@"
}
is equivalent to
alias ls='ls --color=auto'
but IMO less error prone. You also get syntax highlighting.
The only tricky thing is to remember the 'command' prefix if the "alias" has the same name as the command. Otherwise you'll get an infinite loop of the function trying to call itself!
- emmelaich 5y agoNote there is one possibly important difference; aliases are resolved before globbing.
- chubot 5y agoHm what's an example of where you might want to take advantage of that? In practice alias 'lspy=ls *.py' seems to be interchangeable with lspy() { ls *.py "$@" }
- emmelaich 5y agoI have a semi-made desk calculator with prefix notation. You can make it an alias of * , / etc, so ... $ * 4 6 24 $ It would keep it's own stack, but let a persistent process do the actual maths. The persistent process could almost any repl of python, ruby etc. Or use a 'real' calculator like Pike's ivy; https://aplwiki.com/wiki/Ivy https://aplwiki.com/wiki/Ivy
- abathur 5y agoYou can do some wild ~metaprogramming stuff with aliases.
- jolmg 5y agoDo you have examples of something neat?
- 8n4vidtmkvmk 5y agoI've always just written that as \ls to call the non-alias
- emmelaich 5y agoWon't work with functions, so be careful. $ function ls () { > echo denied > } $ ls denied $ \ls denied
- silentprog 5y agoIt generates functions. Though not with a “$@“ at the end, so passing additional arguments at this moment is not supported. I also run a syntax check before the function is added to the file.