Aliasing Over Standard Commands – Why It’s Not Recommended

aliascommand lineshell

For example, a common alias I've seen in the ~/.bashrc file (or equivalents) is

alias rm='rm -i'

However, I've seen people recommend against this because

  1. the alias might not exist on another system and since you've become careless with rm, you inadvertently delete something important. [1]
  2. by using this alias, you in effect train yourself to type y or yes after every rm command, which defeats the whole purpose.

Are there other reasons to recommend against this? Might some programs simply make calls to rm instead of \rm, and aliasing over it could cause problems for them?

I use rm simply as an example, but I've seen other commands like cp or mv covered by aliases as well. Personally, I'm slowly training myself to use an alias like this in place of rm -i:

alias trash=`mv -v -t $HOME/.Trash`

Best Answer

Assuming that you're using bash, this should not cause problems for scripts, as non-interactive bash shells do not source ~/.bashrc or ~/.bash_profile (which is likely either where your aliases are placed, or is the first step to sourcing your aliases in another script). It may, however, cause problems if you are sourcing scripts:

$ alias echo='command echo foo'
$ cat > script << 'EOF'
> #!/bin/bash
> echo bar
> EOF
$ chmod a+x script
$ ./script
bar
$ . ./script
foo bar

Your question covers most of the general concern around aliasing over existing commands, the major one being that unfamiliar environments which appear at first glance to be the same could potentially produce wildly different results. For example, aliasing rm to rm -i has good intentions, but is bad in practise for the reasons you state.

Related Question