DEV Community

Cover image for Creating Custom Aliases with Parameters in Bash: Simplify Your Workflow
Paulo Messias
Paulo Messias

Posted on

Creating Custom Aliases with Parameters in Bash: Simplify Your Workflow

In the world of command-line operations, efficiency is key. Repetitive commands can be streamlined with aliases, but what if you want to pass parameters to those aliases? While a standard alias in Bash does not support parameters, we can leverage shell functions to achieve similar behavior. In this blog post, we will explore how to create simple shell functions that act like aliases but with the ability to accept parameters.

What is an Alias?

In Unix-like operating systems, an alias is a shorthand command that you define to simplify repetitive or complex commands. For instance, instead of typing ls -la, you can create an alias ll that maps to ls -la:

alias ll='ls -la'
Enter fullscreen mode Exit fullscreen mode

However, aliases are limited. They cannot accept parameters, which means they are only useful for static commands. This is where shell functions come in handy.

Shell Functions: Aliases with Parameters

Shell functions allow you to define your own commands and pass parameters to them, offering more flexibility than standard aliases.

Let’s walk through an example of how you can use shell functions to simplify Git commands.


Example: Managing Git Branches with Shell Functions

When working with Git, developers often switch between branches or create new branches. Typing the full command every time can be tedious. For instance:

Before: Typing the Full Command

Creating a new branch:

git checkout -b feature/dashboard
Enter fullscreen mode Exit fullscreen mode

Switching to an existing branch:

git checkout development
Enter fullscreen mode Exit fullscreen mode

By using shell functions, you can create shorthand commands to reduce this effort.

After: Using Custom Shell Functions

  1. Creating a new branch can be shortened with a function:

    Define the function in your ~/.bashrc or ~/.zshrc file:

    new_branch() {
      git checkout -b $1
    }
    

    Now, instead of typing the full command, you can just use:

    new_branch feature/dashboard
    
  2. Switching to an existing branch can be simplified too:

    Define another function:

    branch () {
      git checkout $1
    }
    

    Now, switching branches becomes as easy as:

    branch development
    

Explanation: How it Works

Let’s break down how these shell functions work.

  • In the function new_branch(), the $1 represents the first argument passed to the function. So, when you run new_branch feature/dashboard, $1 becomes feature/dashboard, and the function executes git checkout -b feature/dashboard.

  • Similarly, in the function branch(), the $1 is replaced by whatever argument you provide, so running branch development executes git checkout development.


The Power of Parameters: More Advanced Examples

Shell functions can accept multiple parameters, not just one. For example, let’s say you want to list files in a specific directory:

list_files() {
  ls -l $1
}
Enter fullscreen mode Exit fullscreen mode

You can now use this function to list files in any directory by passing the directory path as an argument:

list_files /path/to/directory
Enter fullscreen mode Exit fullscreen mode

If you want to allow your function to accept multiple parameters (or an arbitrary number), you can use $@, which captures all the parameters passed to the function. Here's an example:

my_command() {
  echo "You passed: $@"
}
Enter fullscreen mode Exit fullscreen mode

Now you can pass any number of arguments, and the function will handle them:

my_command param1 param2 param3
Enter fullscreen mode Exit fullscreen mode

Output:

You passed: param1 param2 param3
Enter fullscreen mode Exit fullscreen mode

How to Make Shell Functions Persistent

To make your shell functions available in every terminal session, add them to your shell configuration file. Depending on your shell, this file might be ~/.bashrc, ~/.bash_profile, or ~/.zshrc. Simply open the file in a text editor and add your functions at the bottom:

new_branch() {
  git checkout -b $1
}

branch() {
  git checkout $1
}
Enter fullscreen mode Exit fullscreen mode

After saving the file, reload it by running:

source ~/.bashrc  # or ~/.zshrc for zsh users
Enter fullscreen mode Exit fullscreen mode

Now, your custom functions will be available in all future terminal sessions!


Conclusion

Shell functions provide a powerful way to enhance your command-line productivity. Unlike aliases, they accept parameters, making them highly flexible. With just a little effort, you can streamline repetitive tasks, reduce typing, and improve efficiency—especially when working with complex commands like Git operations.

By using shell functions like new_branch and branch, you can simplify your workflow and focus more on coding rather than typing out long commands.

Happy coding!

Top comments (2)

Collapse
 
josephj11 profile image
Joe

Nice article.

A few things...

The title is a bit misleading because functions are not aliases.

When you change .bashrc or similar files, the changes only take when a new shell is opened, so if you source the file in one shell session, it doesn't affect any other current sessions. I get caught on this one frequently because I always have three tabs open in konsole and sometimes use shell panels in Dolphin.

Although you want to keep examples simple, your aliases cause more typing than the original command. I name some of my scripts things like print_copies so it's easy to remember what they do and then alias them to things like pc that are easier to type.
One other small thing is that aliases only work at the start of a command, but functions work anywhere.

Collapse
 
paulocappa profile image
Paulo Messias

Hey! Thanks for the feedback and for taking the time to read the article.

You're right about the title being a bit misleading—functions definitely aren't aliases, so I’ll make sure to tweak that for clarity. Also, great point about sourcing .bashrc only affecting the current shell session. I can totally relate to getting caught with multiple tabs open and forgetting that the changes won’t apply everywhere.

As for the aliases, you're absolutely right! The examples I used are just what I’m working with nowadays, but I get how shorter, more practical aliases like yours (e.g., pc for print_copies) can be way more efficient for everyday use. It's a good reminder to always strike that balance between simplicity and practicality.

And thanks for the reminder about aliases only working at the start of a command vs. functions being more flexible. That’s definitely something I should have clarified better.

Thanks again for all the great points! :)