Summary
- The .bashrc file is crucial for configuring commands on Linux shell startup.
- Knowing the shell you’re running helps you configure .bashrc properly.
- Customize your shell with .bashrc by defining new functions, setting aliases, and more.
The shell is so important to Linux, it has its own startup process. Discover how to configure the shell on startup, how to run commands automatically, and what you can do with all this power.
What .bashrc Does and Why
The .bashrc file is typically hidden in your home directory. It is a “run control” file that originated with Bash (Bourne Again Shell), the most popular and widely used Linux shell. Linux uses run control files to script and configure commands when they start, so .bashrc is a startup script for the shell itself.
.bashrc should do whatever is necessary to set up your environment for an interactive shell session. This includes setting environment variables, defining functions, and running utility programs. Once you know how it works, you’ll find various uses to customize and streamline your shell experience.
How .bashrc Works
First of all, know that Bash is not the only shell. On macOS, you’re probably using zsh, a powerful alternative. Most Linux distros use Bash, but there’s no guarantee yours does. To find out which shell you’re running, open a terminal window and run this command:
echo $0
The output should be something like “bash” or “-zsh”, identifying your specific shell.
Next, you may not already have a .bashrc (or equivalent) file, depending on your distro. Try running the following:
ls -l ~/.*rc
This command will show you all hidden files in your home directory that end in “rc”:
If you don’t see .bashrc, you can create an empty file with that name. You can also copy a template from /etc/skel/.bashrc if you have it:
cp /etc/skel/.bashrc ~/.bashrc
The startup process can be quite complicated, so you might want to read about the differences between .bashrc and .profile before you begin, or if you run into any problems.
In the general case, though, you can expect the .bashrc file to run when you open a new terminal, including a new window or tab.
6 Things You Can Do With .bashrc
Because .bashrc is, itself, a Bash script, you can use it for many different purposes. The limit really is your imagination, but the following uses are common.
Run neofetch and Alternatives
Since .bashrc runs when you open a new terminal, it’s a nice place to inject a bit of personality into your terminal. Anything that can act as a welcome message is a nice addition, and neofetch is one of the nicest:
The program shows some colorful ASCII art alongside information about your machine. Apart from being a nice way to begin your terminal session, it’s useful for recognizing that you’re on the machine you think you are—especially useful if you often ssh out to remote machines.
Neofetch is now discontinued, so although you can still use it, you might want to seek out a newer alternative that is being actively developed. Good alternatives include Fastfetch and screenFetch, which are drop-in replacements written in C and Bash script respectively, and onefetch which displays details of a git project you may be working on.
Whichever tool you choose, it’s easy to get it running via .bashrc. First, make sure the plain command works, e.g.
neofetch
Then simply add the same command to .bashrc:
Customize Your Prompt
A more low-key way of customizing your terminal environment is by setting your prompt. This is the bit of text that Linux prints at the beginning of each line in your terminal, prompting you to type something.
The default is fairly useful, but you can customize the prompt to include almost anything. Using the built-in variables, you can include the hostname, user, date, and more. I like this setup:
PS1="\n[\$PWD] \$ "
This adds an extra line which makes it clearer to spot where output from a previous command ends. It also removes the username and host which I don’t really use.
Your default .bashrc probably includes some PS1 configuration already. You can add yours afterward if you want, as I have here:
Set Aliases to Make Commands Easier
Aliasing is another powerful customization that can make your terminal more pleasant to use. By setting an alias for a command, you can give it a more memorable name or one that’s easier to type. You can also use aliases to make it easier to use common combinations of options.
My .bashrc sets several aliases for the ls command:
As with all environmental setup, aliases can be defined in several places, so it’s worth checking the current aliases before you start, using the alias command on its own:
Create Shell Functions for Common Tasks
Even more powerful than aliases, shell functions let you write your own mini-commands. If you define a shell function inside .bashrc, you can run it by typing its name, just like any other command.
Here’s a simple example that you might find saves you some time. It calls mkdir with the useful -p option which creates a full path, including new intermediate directories. It then changes to that new directory:
mkd() {
mkdir -p "$@"
cd "$@" || exit
}
The real win here is that you can now use “mkd” as a substitute for “mkdir”:
Define Environment Variables to Control Commands
Many commands use either global or specific environment variables to alter their behavior. Common widely-recognized env vars include:
- EDITOR which defines your editor program, should another program choose to open it.
- PAGER which defines a program to use to display output taller than one screen.
- BROWSER which defines your default web browser.
But command-specific variables can be just as useful. Take the pager, less, as an example. It supports a LESS variable which you can use to pass default options. For example:
export LESS="--quit-if-one-screen"
Add this to your .bashrc and less will always act nicely, even with files shorter than one screen page. Many commands support similar environment variables that you can set in .bashrc to change their default behavior. Look for a section headed “Environment” or “Environment Variables” in each command’s man page.
Modularize Your Environment by Sourcing Other Files
Once you get into shell customization, your .bashrc file can grow until it’s quite unwieldy. Never mind: this is the perfect opportunity to learn about, and practice, modularization.
Splitting one large file into several smaller ones can make the whole easier to manage, and can enable sharing for other purposes. A well-defined structure can make it easier for others to read, even if future-you is one of those others.
The skeleton bashrc that came with my distro explains how to do this, with clear comments:
In this case, a file at ~/.bash_aliases will be sourced (via the . command) if it exists. The effect will be exactly as if those aliases were directly included in your .bashrc.