Get started with 33% off your first certification using code: 33OFFNEW

The ultimate guide to creating aliases in Bash

2 min read
Published on 26th January 2024

Bash (Bourne Again SHell) is a powerful command-line interface used widely on Linux and macOS. One of its most useful features is the ability to create aliases - shortcuts that can replace longer commands or sequences of commands. This article will delve into how to create various types of aliases in Bash, enhancing your command-line efficiency.

What is a Bash Alias?

An alias in Bash is a shortcut to reference a command or set of commands. It's primarily used to reduce typing, avoid repetition, and simplify complex command structures.

Creating Basic Aliases

To create an alias in Bash, use the alias command followed by the name of the alias, an = sign, and the command you want it to represent in quotes.


alias ll='ls -la'

With this alias, typing ll in the terminal will execute ls -la, displaying a detailed list of files.

Persistent Aliases

By default, aliases are not saved permanently and will be lost when you close the terminal. To make an alias persistent, you need to add it to your ~/.bashrc file (or ~/.bash_profile on macOS).


echo "alias ll='ls -la'" >> ~/.bashrc
source ~/.bashrc

Aliases with Parameters

Aliases don't inherently support passing parameters. However, you can use functions to create a similar effect.


function search() {
  grep -r "$1" .
alias srch=search

Here, srch is an alias for a function that searches for a string in all files in the current directory.

Batch Command Aliases

You can combine multiple commands in one alias using the && operator or semicolons.


alias update='sudo apt update && sudo apt upgrade'

This alias runs an update and then an upgrade for APT package manager.

Complex Aliases

For more complex operations, consider using a shell script instead of an alias. However, you can still create sophisticated aliases using functions.


function mkcd() {
  mkdir -p "$1" && cd "$1"
alias mcd=mkcd

This alias creates a new directory and navigates into it.

Tips for Creating Useful Aliases

  1. Keep Them Short: The purpose of an alias is to save time. Keep your aliases short and memorable.
  2. Don’t Overwrite Existing Commands: Avoid using names of existing Unix commands unless you intend to permanently replace them.
  3. Use Comments: In your .bashrc or .bash_profile, comment your aliases for future reference.
  4. Test Before Adding: Before making an alias permanent, test it in your current session.

Creating Bash aliases is an effective way to enhance your productivity on the command line. Whether it’s a simple command shortcut or a complex function, aliases can significantly streamline your command-line operations. As you grow more comfortable with the command line, customizing your environment with aliases tailored to your workflow becomes an invaluable skill.