Create `setup_alias.sh` for localized alias available in the working directory only

sulmanweb

Sulman Baig

Posted on January 7, 2023

Create `setup_alias.sh` for localized alias available in the working directory only

If you find yourself constantly typing out long commands or navigating to specific directories in your terminal, setting up aliases can be a huge time-saver. However, sometimes you only want these aliases to be available within a particular working directory. In this case, you can create a script called setup_alias.sh that will allow you to set up localized aliases that can only be used within that working directory.

To create the script, open up a terminal and navigate to the working directory where you want the localized aliases to be available. Then, create a new file called setup_alias.sh using the touch command:

touch setup_alias.sh
Enter fullscreen mode Exit fullscreen mode

Next, open the file in a text editor (such as Nano, Vim, or VS Code). Inside the file, you can define your localized aliases using the alias command. For example:

alias ls='ls -la'
alias cd..='cd ..'
alias dcbe='docker-compose run --rm api bundle exec $*'
alias rubocop='dcbe rubocop -A'
... 
Enter fullscreen mode Exit fullscreen mode

You can add as many aliases as you like to the script, each on its line. Once you have finished defining your aliases, save the file and exit the text editor.

You will need to run the script to make the aliases available in your terminal. You can do this by typing the following command into your terminal:

source setup_alias.sh
Enter fullscreen mode Exit fullscreen mode

This will run the script and make the aliases available in your terminal. You will only be able to use these aliases within the working directory where the hand is located. If you navigate to a different directory, the aliases will no longer be available but other aliases in another directory will be available.

To make the aliases available every time you open a new terminal window in that working directory, you can add the source command to your .bashrc or .zshrc file. This file is run every time you open a new terminal window, so the aliases will be available whenever you need them. Open the .bashrc or .zshrc file in a text editor and add the following line at the end:

if [ -f "$PWD/setup_alias.sh" ]; then
  source "$PWD/setup_alias.sh"
fi
Enter fullscreen mode Exit fullscreen mode

By creating a setup_alias.sh script, you can easily set up localized aliases that are only available within a specific working directory. This can be a great time-saver if you frequently work with a specific set of commands or directories in one specific location.


Happy Coding!

💖 💪 🙅 🚩
sulmanweb
Sulman Baig

Posted on January 7, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related