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:
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:
Increased productivity: By reducing the time spent typing repetitive commands, you can focus more on coding and development tasks.
Customization: Bash aliases are entirely customizable, tailored to your specific needs and preferences.
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:
Open your terminal.
Navigate to your home directory (
cd ~
) if you're not already there.Edit your shell configuration file using a text editor like Nano or VSCode. For example, to edit
.bashrc
, usenano ~/.bashrc
orcode ~/.bashrc
.Add your alias definitions to the file. The syntax for creating an alias is:
alias shortcut='command'
. Replaceshortcut
with the name of your desired alias andcommand
with the actual command or commands you want to associate with the alias.Save the file and exit the text editor.
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.
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:
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.
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
, it 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. let’s 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:
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
Let’s 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:
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
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!