What's a bashrc? What's a alias?
- .bashrc is the configuration file for bash, a linux shell/command interpreter.
- An alias is a substitute for a (complete) command. It can be thought of as a shortcut.
- .bashrc is found in the home folder of a user ( ~ ) . It is a hidden file, to see it show hidden files in your file manager or use ls -a
Backup your current ~/.bashrc
It can be useful to backup the ~/.bashrc before editing it, as it allows one to be able to easily recover from the unexpected. To make a backup of your current .bashrc . Open a terminal and execute:
The original .bashrc can be restored with by executing
Any changes made to the ~/.bashrc will have no effect on any currently open terminal windows. To test newly updated changes in your ~/.bashrc open a new terminal or use the command:
Aliases can turn a complex command string into a simple custom made command that one can type in the Terminal.
Creating aliases in bash is very straight forward. The syntax is as follows:
For updating your system
To upgrade the system via pacman, the command used is
This can be aliased in ~/.bashrc with
To upgrade packages installed from the AUR via pamac, the command used is
This can be aliased with
For editing commonly used files
To edit ~/.bashrc itself and automatically reload bash configuration file (so that changes made to .bashrc can be implemented in current terminal session)
To edit /etc/fstab
To edit /etc/default/grub
To update GRUB
To update your grub bootloader using the sudo update-grub
Creating Bash Aliases with Arguments (Bash Functions)
Sometimes you may need to create an alias that accepts one or more arguments. That’s where bash functions come in handy.
The syntax for creating a bash function is very easy. They can be declared in two different formats:
To pass any number of arguments to the bash function simply, put them right after the function’s name, separated by a space. The passed parameters are $1, $2, $3, etc., corresponding to the position of the parameter after the function’s name. The $0 variable is reserved for the function name.
Let’s create a simple bash function which will create a directory and then navigate into it:
Now instead of using mkdir to create a new directory and then cd to move into that directory , you can simply type:
Keeping bash alias in a different file
Bash allows you to add local aliases in your ~/.bashrc file. To do this create a file called ~/.bash_aliases and add these contents in your ~/.bashrc file:
Now you can add any aliases in your ~/.bash_aliases file and then load them into your Bash session with the source ~/.bashrc command.
This list is not comprehensive. Almost anything that is commonly used can be shortened with an alias