Bash Aliasing

Tadios Abebe | Feb 18, 2024 min read

If you find yourself repeatedly typing long commands in your terminal, Bash aliases are a quick and powerful way to streamline your workflow. They allow you to create custom shortcuts for frequently used commands, making your day to day operations faster and more efficient.

What is a Bash Alias?

A Bash alias is a custom shortcut that references another command. Instead of typing a long or complex command each time, you can define a simple alias to run it for you.

For example:

alias ll="ls -alF"

Now typing ll in the terminal will execute ls -alF.

Creating a Temporary Bash Alias

You can create a quick, temporary alias directly in your shell:

alias shortname="custom command"

Example:

alias gs="git status"

This alias will only last for the current terminal session. Once you close the terminal, it will be gone.

Creating a Permanent Bash Alias

To make your alias persist across sessions, you need to add it to your shell configuration file typically ~/.bashrc.

You can do this manually or via the terminal:

echo "alias shortname='custom command'" >> ~/.bashrc

Example:

echo "alias gs='git status'" >> ~/.bashrc

Then, reload your .bashrc to apply the changes:

source ~/.bashrc

Removing a Bash Alias

To remove an alias from your current session, use:

unalias shortname

To remove a permanent alias, delete the corresponding line from your ~/.bashrc file and reload it:

nano ~/.bashrc
source ~/.bashrc

Pro Tips

  • Use aliases for long or error prone commands like docker, kubectl, ssh, or rsync.
  • Group your aliases in a separate file (like ~/.bash_aliases) and source it from ~/.bashrc:
    if [ -f ~/.bash_aliases ]; then
        . ~/.bash_aliases
    fi
    
  • Avoid naming aliases the same as existing commands unless intentionally overriding.
comments powered by Disqus