Improving Your Workflow as a Developer with Bash Aliases
Tags: bash, terminal, git, productivity
Published: 2023-08-05 08:00:22

---
title: Improving Your Workflow as a Developer with Bash Aliases
published: true
date: 2023-08-05 07:47:54 UTC
tags: bash, terminal, git, productivity
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wulhjkhehaeus38dqbhi.png
canonical_url: https://blog.jamin.sh/improving-your-workflow-as-a-developer-with-bash-aliases
---

As a developer, you most likely spend a significant amount of time working with the command-line interface (CLI). The command line is a powerful tool that allows you to interact with your operating system and execute various tasks efficiently. However, typing long and repetitive commands can become tedious and time-consuming. Fortunately, Bash aliases can help you streamline your workflow and boost productivity. This article will explore what Bash aliases are and how you can use them to enhance your development process.

## Prerequisite

Before you begin, this article requires basic knowledge of the terminal/CLI and bash commands.

## What is a Bash Alias?

A bash alias is a user-defined shortcut for frequently used commands. When you create an alias, you assign a custom name to a command or a series of commands. Once defined, you can use the alias instead of typing the entire command. Aliases are specific to your user profile and can be stored in your shell's configuration file, such as `.bashrc` or `.bash_profile`.

For example, the following alias command creates a shortcut for the `ls -al` command:

```bash
alias ls="ls -al"

```

This shortcut can then be used to list all of the files and directories in a directory. So, instead of writing the longer command `ls -al` every time you need to list the files in a directory, you can now type `ls`.

## Benefits of Using Bash Aliases

There are several benefits to using Bash aliases:

1. **Increased productivity** : By reducing the time spent typing repetitive commands, you can focus more on coding and development tasks.

2. **Customization** : Bash aliases are entirely customizable, tailored to your specific needs and preferences.

3. **Error prevention** : Since aliases can be designed to avoid complex or risky commands, you can reduce the chances of making mistakes when running important commands.

## Creating a Bash Alias

To create a Bash alias, follow these steps:

1. Open your terminal.

2. Navigate to your home directory (`cd ~`) if you're not already there.

3. Edit your shell configuration file using a text editor like Nano or VSCode. For example, to edit `.bashrc`, use `nano ~/.bashrc` or `code ~/.bashrc`.

4. Add your alias definitions to the file. The syntax for creating an alias is: `alias shortcut='command'`. Replace `shortcut` with the name of your desired alias and `command` with the actual command or commands you want to associate with the alias.

5. Save the file and exit the text editor.

6. To make your changes take effect, restart your terminal or run the command `source ~/.bashrc`.

## Aliases with Multiple Commands

Using logical operators such as the AND (`&&`) and OR (`||`) operators, you can create aliases with multiple commands.

```bash
alias shortcut='command1 && command2'

```

### Using the AND (`&&`) Operator

The double ampersand is a logical AND operator used to execute the second command only if the first command succeeds (i.e., returns an exit status of 0). However, if `command1` fails (returns a non-zero exit status), `command2` will not be executed. Here's an example alias in Bash:

```bash
alias npmsetup='npm install && npm run dev'

```

In this example, you've created an alias called `npmsetup`. When you execute `npmsetup`, it will first install all the packages in the project using the command `npm install`. If the installation is successful, it will then run the development server using the `npm run dev` command.

### Using Semicolon (`;`)

In Bash, the semicolon is a simple command separator. When you use it to separate commands, Bash will execute each command sequentially, regardless of whether the previous command succeeded or failed. This means that all commands separated by semicolons will be executed, even if some of them produce errors.

```bash
alias shortcut='command1 ; command2 ; command3'

```

In this case, `command1` will be executed first, followed by `command2`, and then `command3`, irrespective of whether any of them fail or succeed.

### Using the OR (`||`) Operator.

The double pipe is a logical OR operator used to execute a second command when the first command fails: When you use `command1 || command2`, Bash will execute `command1` first. If `command1` fails (returns a non-zero exit status), then Bash will proceed to execute `command2`. However, if `command1` succeeds (returns an exit status of 0), `command2` will not be executed. lets have a look at an example of using the `||` operator:

Imagine you want to create an alias command `dev` that turns on the development server in a JavaScript/Typescript project, using the `||` operator you can create the alias as follows:

```bash
alias dev='npm run dev || npm run develop'

```

In this example, you've created an alias called `dev`. When you execute `dev`, it will first try to run the development server using the command `npm run dev`. If that command is not found or fails, it will then try the second command `npm run develop`.

## Bash Alias Examples

Lets consider some practical examples of bash alias commands that can help improve your development workflow.

I use the following alias commands to run common git and development commands with just a few keystrokes without having to type the whole command:

```bash
alias gm='git commit -m'
alias pull='git pull'
alias push='git push'
alias gs='git status -sb'
alias gc='git checkout'
alias gb='git branch'
alias gline='git log --oneline'
alias gcp='git cherry-pick'
alias add='git add'
alias addall='git add .'
alias dps='docker ps'
alias dcu='docker-compose up'
alias cf='STAGE_BACKEND=true yarn start'
alias dev='npm run dev || npm run develop'
alias clean='npm run clean'

```
What aliases do you use to speed up your workflow? Please share in the comments section; I'll be waiting :)

## Reference

- [Bash scripting for beginners](https://www.freecodecamp.org/news/bash-scripting-tutorial-linux-shell-script-and-command-line-for-beginners/)

- [Advanced Bash-Scripting Guide](https://tldp.org/LDP/abs/html/aliases.html)

- [Install Bash on Windows](https://itsfoss.com/install-bash-on-windows/)

## Conclusion

Bash aliases can help you optimize your workflow as a developer. By creating shortcuts for common commands, you can save time, improve productivity, and make your development experience more enjoyable. So, don't hesitate to start creating your custom aliases today to improve your productivity on your daily development tasks.

Happy coding!

I'd love to connect with you on [**Twitter**](https://twitter.com/0xJamin) | [**LinkedIn**](https://www.linkedin.com/in/trust-jamin/) | [**GitHub**](https://github.com/0xJamin/)