When to Use Alias, Script, or Function in Bash

aliasbashfunctionshell-script

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.

Best Answer

An alias should effectively not (in general) do more than change the default options of a command. It is nothing more than simple text replacement on the command name. It can't do anything with arguments but pass them to the command it actually runs. So if you simply need to add an argument at the front of a single command, an alias will work. Common examples are

# Make ls output in color by default.
alias ls="ls --color=auto"
# make mv ask before overwriting a file by default
alias mv="mv -i"

A function should be used when you need to do something more complex than an alias but that wouldn't be of use on its own. For example, take this answer on a question I asked about changing grep's default behavior depending on whether it's in a pipeline:

grep() { 
    if [[ -t 1 ]]; then 
        command grep -n "$@"
    else 
        command grep "$@"
    fi
}

It's a perfect example of a function because it is too complex for an alias (requiring different defaults based on a condition), but it's not something you'll need in a non-interactive script.

If you get too many functions or functions too big, put them into separate files in a hidden directory, and source them in your ~/.bashrc:

if [ -d ~/.bash_functions ]; then
    for file in ~/.bash_functions/*; do
        . "$file"
    done
fi

A script should stand on its own. It should have value as something that can be re-used, or used for more than one purpose.