How to bypass aliases that conflict with bash commands
Table of contents
A Note on the Series:
Fast Fridays 🏎 is a series where you will find fast, short, and sweet tips/hacks that you may or may not be aware of. I will try to provide these (fairly) regularly on Fridays.
It's common to have aliases in dotfiles that offer shortcuts for running frequent commands. A useful trick is to create an alias for an existing bash command and use that instead of the actual command. Occasionally, you might need to run the real command instead of the alias. In this Fast Friday tip, you'll learn how to easily bypass an aliased command.
Simplifying Commands with Aliases
I have an alias defined in my .zshrc
file that maps rm
to the trash
command, a third-party package that moves files and folders to the trash. Essentially, it’s a safer alternative to the dangers of the default rm
command as it moves anything you delete to the trash folder instead of removing it entirely from your computer without any way to retrieve it.
P.S. This was a tip that I got from the following Wes Bos video years ago:
Although it’s rare that I find myself removing something unintentionally, it surely came in handy the one time I fat-fingered a command and deleted an entire directory of projects. Thankfully, all I had to do was CTRL + Z to undo the action, and all my folders and files were returned to their original location. Phew! 😥 Crisis averted!
Bypassing Aliases
Even though I have those safeguards in place, there are times when I do actually want to run the default rm
command. A common example of this is when I’m copying and pasting a command from a work repo and it includes the -rf
flags. If I were to run a command that included those flags I would get an output that reads “The file “-rf” doesn’t exist.” That’s because the aliased version runs trash
which doesn’t accept those flags.
> rm -rf blog-temp
> The file “-rf” doesn’t exist.
In order to get around this, you have two options:
\
command
By prefixing with the \
(backslash) or command
keyword, you instruct the shell to bypass any alias and execute the original command.
For example, if I were to run either of the following commands it would correctly remove the specified directory:
> command rm -rf blog-temp
> \rm -rf blog-temp
This Fast Friday tip is a bit niche, but it's super handy when needed!
El Fin 👋🏽
If you enjoy what you read, feel free to like this article or subscribe to my newsletter, where I write about programming and productivity tips.
As always, thank you for reading, and happy coding!
Subscribe to my newsletter
Read articles from Alyssa Holland directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Alyssa Holland
Alyssa Holland
Welcome to my blog 👋🏽 I'm a Front-End Developer with a passion for learning! I write about Programming 👩🏽💻 and Productivity Tips ✅