Bash Aliases
reusable scripts with bash
When you are using a shell like bash you are likely using a lot of commands. These commands can be quite long. A useful way to deal with commands that you use a lot is to make a shortcut to use them. This way you have to type less.
In this post we will look at how to make these shortcuts for bash and how you might organize them. We will also look at how to make specific shortcuts for 2 programs.
Bash shortcuts
To create a shortcut in bash you can add the following line to your ~/.bashrc file.
alias ll="ls -la"
This means that if you execute ll in the terminal the ls -la command will be executed.
To see these change have effect you first have to relogin or execute:
source ~/.bashrc
Organizing the shortcuts
The ~/.bashrc file can quickly become cluttered when all the shortcuts are places inside this file.
To solve this we have multiple options. For example: we can create one seperate file to put all the aliases in or we can
have a folder which contains multiple files of aliases.
In the following example we will create a folder to seperate all our shortcuts over multiple files.
First add the following lines to the ~/.bashrc folder to have it read all the files from the folder. and load these
|
|
Create the ~/.bashrc.d folder with
mkdir ~/.bashrc.d
In this directory you can create as many files as you want to order you shortcuts. Lets for example create a file for our docker shortcuts.
touch ~/.bashrc.d/docker.bashrc
In this file we will add our shortcuts. Your shortcuts don’t have to be created with alias. It is also possible to use a
bash function.
To see how this works we add the following lines to ~/.bashrc.d/docker.bashrc.
|
|
after we execute source our ~/.bashrc we can use the shortcut. The shortcut removed all docker containers on the
system.
docker_rmc_a
Git shortcuts
Git offers us the option to have special git aliases. In the ~/.gitconfig you can add the following:
[alias]
fp = push --force-with-lease
This will allow you to force push you code with the following command:
git fp
Organizing the git shortcuts
It is possible to have the git shortcuts in a different file and include this file in the ~/.gitconfig.
Create a file called .gitconfig.aliases:
touch ~/.gitconfig.aliases
Move the alias section from ~/.gitconfig to ~/.gitconfig.aliases. In the ~/.gitconfig add the following:
[include]
path = ~/.gitconfig.aliases
It is also possible to only load a git configuration file based a condition. For example if the git repo you are in is in the ~/Documents/work/ directory.
The trailing slash means that git will add ** and thus it will work for all the repo’s under this directory.
In code it looks like this:
[includeIf "gitdir:~/Documents/work/"]
path = ~/.gitconfig.aliases
AWS cli shortcuts
The AWS cli offers you the option to create AWS specific shortcuts. To create these you create the ~/.aws/cli/alias file.
touch ~/.aws/cli/alias
In the file you can include the following code:
|
|
The top link is for an alias file that AWS developers use them self. The 2 other commands are from the AWS file and do the following:
- Show which IAM identity is calling
- Login to AWS ECR (container registry)
You can use them in the following way:
aws docker-ecr-login
Reuse shortcuts over multiple machines
If you have multiple machines on which you would like to use them same shortcuts you can do the following:
- Create a git repo
- Save the files in there
- Create simlinks to the files in the git repository for example:
ln -s ~/Documents/configs/.aws.alias ~/.aws/cli/alias
Conclusion
In this post you have seen multiple ways to create shortcuts to be more efficient as developer. Have fun and success applying them in your use-cases.