Fundamentals 8 min read

Mastering Bash Aliases: Avoid Pitfalls and Make rm Safer

This article explains how to use Bash aliases safely, highlights common mistakes such as improper argument handling and command chaining, and shows how to replace risky aliases with shell functions for more reliable file deletion.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Mastering Bash Aliases: Avoid Pitfalls and Make rm Safer

1.1 Common alias usage

By default rm is an alias for rm -i, and ll is an alias for ls -l. You can create custom aliases to replace commands with specific options or combine multiple commands. For example: alias ls='ls -lA' This lists hidden files as well.

Running alias without arguments shows all defined aliases in the current shell.

If an alias shares the same name as a command, the alias takes precedence, which you can verify with which:

which mv
alias mv='mv -i'
/bin/mv

When an alias and the original command have the same name, you must delete the alias, use the absolute path, or escape the name to run the original command.

Temporary aliases are defined with alias; to make them permanent, add the definition to /etc/profile, ~/.bash_profile, or ~/.bashrc. Remember to reload the file with source. Use unalias to remove an alias temporarily.

1.2 Limitations of aliases

Aliases can be ambiguous. For instance, the following alias attempts to back up files before deleting them: alias rmm='cp $@ ~/backup;rm $@' According to the Bash manual, only the first command ( cp) is part of the alias; the rm after the semicolon runs as a separate command after alias expansion. Moreover, the $@ variable is only expanded for the last command, so the cp receives no arguments. alias rmm='cp ~/backup;rm $@' You can verify this behavior with echo tests.

alias rmm='echo cp $@ ~/backup;echo rm $@'
rmm /etc/fstab /etc/hosts
cp /root/backup
rm /etc/fstab /etc/hosts

When an alias contains only a single command, it still may not work as expected if arguments are placed after a path like ~/backup, because $@ is consumed by the preceding argument.

alias rm='mv -f $@ ~/backup'

Therefore, aliases are best suited for simple command or argument substitution. The Bash manual recommends using shell functions for more complex tasks.

1.3 Best practices for safe command replacement

Writing a shell script or function is safer and more robust than relying on aliases. You can define a function that backs up files before deletion:

function rm() {
  [ -d ~/rmbackup ] || mkdir ~/rmbackup
  /bin/mv -f "$@" ~/rmbackup
}
export -f rm

Because functions receive the full $@ argument list, they can handle parameter expansion correctly, unlike aliases.

If you need to call the original rm command after defining a function with the same name, use command rm or the absolute path /bin/rm.

When scripting, it is also advisable to change to the target directory before invoking rm and use relative paths to avoid accidental deletions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ShellSafetyScriptingBashrmAlias
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.