It's taken me almost 10 years of Linux usage to ask this question. It was all trial and error and random late-night internet surfing.
But people shouldn't need 10 years for this. If I were just starting out with Linux, I'd want to know: When to alias, when to script, and when to write a function?
Where aliases are concerned, I use aliases for very simple operations that don't take arguments.
alias houston='cd /home/username/.scripts/'
That seems obvious. But some people do this:
alias command="bash bashscriptname"
(and add it to the .bashrc
file)
Is there a good reason to do that? I'm trying really hard, but I genuinely can't think of any circumstances in which I'd want to do that. So, if there is an edge case where that would make a difference, please answer below.
Because that's where I would just put something in my PATH and chmod +x
it, which is another thing that came after years of Linux trial-and-error.
Which brings me to the next topic. For instance, I added a hidden folder (.scripts/
) in the home directory to my PATH by just adding a line to my .bashrc
(PATH=$PATH:/home/username/.scripts/
), so anything executable in there automagically autocompletes.
If I needed to.
I don't really need that, though, do I? I would only use that for languages that are not the shell, like Python.
If it's the shell, I can just write a function inside the very same .bashrc
:
funcname () {
somecommand -someARGS "$@"
}
As I stated, I found a lot of this out through trial and error. And I only truly saw the beauty of functions when my computer died and I was forced to use the computers of the people around me when they weren't using them.
Instead of moving a whole directory of scripts from computer to computer, I ended up just replacing everyone else's .bashrc with my own, since they had never even made a single modification.
But did I miss anything?
So, what would you tell a beginning Linux user about when to alias, when to script, and when to write a function?
If it's not obvious, I'm assuming the people who answer this will make use of all three options. If you only use aliases, or only use scripts, or only use functions—or if you only use aliases and scripts or aliases and functions or scripts and functions—this question isn't really aimed at you.