Linux and related distributions like Ubuntu, Debian, CentOS, Fedora, RedHat, … provides shell to manage systems. By default bash shell is used by the most of the distributions. Every shell in Linux have some configuration, environment variables, etc. We generally need to change them after opening a new shell.
.bashrc is used to set shell related configuration during new shell creation. In every new shell
.bashrc file is fired before we get the new shell. In this tutorial we will look how to use
.bashrc provides very flexible way to edit new shell configuration. Each user will have the
.bashrc file in their home directory.
.bashrc is a hidden file and not listed with
ls or File managers. But we can edit it just providing the path like below.
Following command will open .bashrc of current user with nano text editor
$ nano ~/.bashrc
Which will look like below.
Add New Path Variable
One of the most popular use case for add new Path for the current user is adding to the .bashrc . In this example we will add path
/opt/bin/ as new path for the current user.
While running complex commands writing them again and again is redundant task. Alias can be used to create shortcuts in the command line. Another useful usage for
.bashrc is defining new alias for the current user. In this example we add alias for
alias mv='mv -i'
Linux provides prompt for each interactive shell. This generally provides the user name , host name and path. We may want to use more simple prompt just like
$ we can set from .bashrc with the following command.
export PS1="$ "
Source Other Configuration File
We may need to load other configuration and shell startup file other than .bashrc. In this situations we can use .bashrc too.
if [ -f /etc/bashrc ]; then . /etc/bashrc fi
Bashrc vs Bash_profile
There is an alternative to bashrc file which is named bash_profile . bash_profile provides global configuration for all users. Bashrc is limited with current user.
Sometimes we need to reload bashrc file without logout and login. We can use source mechanism to reload bashrc. One of the following commands can be used to reload bashrc.
$ source ~/.bashrc
$ . ~/.bashrc