What Is .bashrc?
Something you may have come across if you’ve been looking around the filesystem on your Linux server is that each user’s home directory will contain a number of “hidden” files. On Linux systems, hidden files have a filename that starts with a dot (.). The purpose of having these as hidden files is to reduce visible clutter on the filesystem by removing system and configuration files from view when they aren’t necessary. A user’s home directory will contain a number of hidden files and directories which consist of configuration information for various programs that may use per-user configuration. One such program that can use per-user configuration is bash, and there are a number of files stored in a user’s home directory for it, one of which is “.bashrc”.
Using .bashrc
So we’ve tentatively established that “.bashrc” is a file that bash uses to allow per-user configuration, but it has a relatively specific use. The man files state that “.bashrc” is read specifically when bash is run to provide an interactive shell that isn’t a login shell. This means that if you run the command “/bin/bash” at the command line it will be read, but not when bash is run to be your terminal as you log into a system. By contrast, if it exists the file “.bash_login” is used to provide configuration for your login shell, along with “.bash_profile” and “.profile”. These days, most of the popular Linux distributions, including CentOS, Debian, Red Hat and Ubuntu, make use of the “.bashrc” file for both when bash is used as a login shell, and when it isn’t by using either “.bash_profile” or “.profile” to include the contents of “.bashrc” for login shells. This means that the “.bashrc” file has become the default single configuration point for interactive bash sessions on most Linux distributions.
Due to its modern usage, “.bashrc” has a lot of configuration details inside for your user session. For example, the configuration of your command-line prompt is handled in there, as are path configurations for the directories that bash should look in to find commands that are called without a full or relative path supplied. Some command aliases may be set up, though often the file will include the contents of a “.bash_aliases” file if it exists to allow the user to keep their aliases separate. On Debian-based systems, it also contains configuration for the bash history and auto-completion tools in bash.
.bashrc Options
That’s a fair number of tasks that are already handled by this file, and you can reconfigure it to tweak a number of settings that you might need to change for your own setup. One example is if you have manually installed an application you can add its path here to your path environment variable. Alternatively, you can set up your own environment variables in the file that you need to have set the next time you start a session. You can also create functions that you can call in similar to a command that will perform multiple commands for you.
The file itself is set up like any other bash script, and you can add to it by adding commands just as you would execute them at the command line, or you can use bash’s scripting language to perform more complicated functions as required. This will be called every time that you open a new bash terminal, such as when you open some in screen, so it’s worth being mindful of this if you are thinking of including commands that perform tasks when you open the terminal. For this reason it’s best, to ensure that you stick to only including commands and scripts that serve to set up the terminal for interactive use and use a different method to trigger other commands.