- Published on
How to Conditionally Set Shell Alias in Zsh or Bash
- Authors
- Name
- Ashik Nesin
- @AshikNesin
Lately, I've started using pnpm for my personal projects however it's not playing well with some of the legacy work-related items.
The problem is that I'm using dotfiles for all my configuration which is shared across my work and personal laptop.
So I wanted to set it conditional. In my case, I want to check for the current username and set the alias accordingly.
Here's how I did it. I'm using Zsh, so I've placed it in ~/.zshrc
file
# Run "echo $HOME" -> To get the path
if [[ $HOME == *"/Users/YourUserName"* ]]; then
alias npm="pnpm"
fi
So the above snippet will check for my home directory. And if it matches the one that I've configured then it'll set the alias npm to pnpm.
Happy conditional alias!