Simplified Guide to Bash Aliases

In Linux, an alias is a custom shortcut for a command, letting you run a long or frequently used command with just a few keystrokes. This guide covers creating aliases, making them permanent so they survive a reboot, and loading them into your shell.

Step 1: Open the ~/.bash_aliases File

Keeping your aliases in a dedicated ~/.bash_aliases file is tidier than putting them straight into your .bashrc. Open it in an editor:

nano ~/.bash_aliases

Step 2: Define Your Alias

Add an alias in the form alias name="command". For example, this makes cds jump straight to your Scripts folder:

alias cds="cd ~/Scripts"

Replace the command in quotes with whatever you want the alias to run.

Step 3: Check That ~/.bashrc Loads ~/.bash_aliases

Most distributions already include a snippet in ~/.bashrc that loads your aliases file. Open it and look for a block like the one below:

nano ~/.bashrc
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

If it’s already there, your aliases file is being loaded and you can skip the next step.

Step 4: Add the Loader (If It’s Missing)

If you didn’t find that block, add it to the end of your ~/.bashrc so your aliases file is sourced every time a shell starts:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Step 5: Reload Your Shell

Aliases are loaded when a shell starts, so to use them right away without opening a new terminal, reload your config:

source ~/.bashrc

Your alias is now ready. Type it just like any other command:

cds

From here you can build up a whole set of aliases for the commands you type most, and because they live in ~/.bash_aliases, they’ll stick around and stay easy to manage.

Related guides


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *