[Linux] bashrc
Published:
In this post, the concept of .bashrc is introduced.
.bashrc
When a user first runs bash, it is designed to execute a specific set of files. These files can vary and be executed depending on the context. However for now, we will only consider the .bashrc file located in the home directory. By customizing the .bashrc file, we can modify our shell’s start-up configuration.
$PATH
In order to add locations to $PATH, you can do as follows. Click here to know more about PATH.
- open .bashrc file
vi ~/.bashrcNotice that ‘~’ means the home directory.
- add PATH
export PATH = "$PATH:/your/new/path"“$PATH:” means you append your new directory to existing $PATH data. If you do not write it, it will overwrite.
- source .bashrc
source ~/.bashrc
alias
There are several ways to make alias. You can find out current aliases applied by typing ‘alias’.
temporary way
Easily, you can use the alias/unalias command to make alias as below. However, this alias will not exist when you exit and rerun the shell in the future. 
update .bashrc
You can make permanent alias by configuring the .bashrc file. You can do so by as below.
- open .bashrc file
vim ~/.bashrc - add alias
alias t='cd ~/environment/hello_linux; cat test'Notice that there should be no black or tabs between “=” after alias name and “ ‘ “.
- source .bashrc
source ~/.bashrc
make .bash_aliases
You can make .bash_aliases file under home directory that defines your aliases and apply it to .bashrc file. The name .bash_aliases is a convention, you can use different name. Your .bash_aliases file may look like this.
alias t='cd ~/environment/hello_linux; cat test'
alias c='clear'
alias l='ls'
function test{
if [ -f $1 ] ; then
COMMAND
else
COMMAND
fi
}
You can also define functions in it, but more details later. Now, you have add certain lines to your .bashrc file to use .bash_aliases.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
The grammar of if statement is as below. We check the existence of file with certain names with -f.
if [condition]; then
code to execute
fi

Leave a Comment