logo logo

The next-generation blog, news, and magazine theme for you to start sharing your stories today!

The Blogzine

Save on Premium Membership

Get the insights report trusted by experts around the globe. Become a Member Today!

View pricing plans

New York, USA (HQ)

750 Sing Sing Rd, Horseheads, NY, 14845

Call: 469-537-2410 (Toll-free)

hello@blogzine.com

What Is .bashrc and How You Can Customize It

avatar
Louis Ferguson

An editor at Blogzine


  • 🕑 5 minutes read
  • 7 Views
What Is .bashrc and How You Can Customize It

There are a number of hidden files tucked away in your home directory. If you run macOS or a popular Linux distribution, you’ll see a file named “.bashrc” up near the top of your hidden files. Here we explain what does the. bashrc file do and why you should editing and customize it.

What is. bashrc

If you run a Unix-based or Unix-like operating system, you likely have bash installed as your default terminal. While many different shells exist, bash is both the most common and, likely, the most popular. If you don’t know what that means, bash interprets your typed input in the Terminal program and runs commands based on your input. It allows for some degree of customization using scripting, which is where. bashrc comes in.

A screenshot of the bashrc file inside a file manager.

The. bashrc file contains a list of customization options for the bash shell. It is loaded at each launch. The. bashrc file is found in the Home user directory. The. (dot) in front of the file name means that it is hidden from plain view. You will need to activate “View Hidden Files” option to view it.

How can I edit. bashrc

You can edit. bashrc in any text editor. You can also use nano to edit it in the Terminal.

nano ~/.bashrc

If you’ve never edited your. bashrc file before, you might find that it’s empty. That’s fine! If not, you can feel free to put your additions on any line.

Edits in. bashrc have to follow bash’s scripting format. If you don’t know how to script with bash, there are a number of resources you can use online. This guide represents a beginner-friendly introduction into the aspects of Bash that we are not mentioning here.

Any changes you make to. bashrc will be applied next time you launch terminal. If you want to apply them immediately, run the command below:

source ~/.bashrc

.bashrc Customization Tips

There’s a couple of useful tricks you can do to make your terminal experience more efficient and user-friendly.

1. Aliases

Aliases allow you to create a shorter version of a long command.

For example, the command ls is often used to display the contents of your directory. You can also use ls -lha to display the content with more details. Now there is an alias ll, which is set to run ls -lha. You can just type ll in the terminal and it will run ls -lha command.

A terminal showing a list of basic aliases.

You’ll need to format your aliases like so:

alias ll="ls -lha"

Type the text you want to replace on the left, and the command on the right between quotes. You can use to this to create shorter versions of command, guard against common typos, or force a command to always run with your favored flags. You can also circumvent annoying or easy-to-forget syntax with your own preferred shorthand.

2. Functions

In addition to shorthand command names, you can combine multiple commands into a single operation using bash functions. They can get pretty complicated, but they generally follow this syntax:

function_name () {
command_1
command_2
}

For example, the command below combines mkdir and cd. Typing md folder_name creates a directory named “folder_name” in your working directory and navigates into it immediately.

md () {
mkdir -p $1
cd $1}

The $1 you see in the function represents the first argument, which is the text you type immediately after the function name.

3. Adding a Simple Bash Prompt

You can also use the. bashrc file to create a custom shell prompt. The shell prompt is a line of text that shows up at the start of every terminal input line. This can contain either static information such as your system’s name or dynamic scripts that change depending on the shell’s current working directory.

Here are several interesting bash prompts you can add to your. bashrc file.

4. Modify the PATH Variable

The PATH variable is an important variable that tells your shell where to find scripts and binaries for the current session. You can use the. bashrc file to store/change the content of your PATH variable. This is useful in cases where you want to run your own programs from a custom directory.

To modify the PATH variable, add the following line of code to the end of your. bashrc:

PATH="$PATH:"

This will tell your shell to load the default value of PATH before loading any custom arguments.

A screenshot showing the PATH variable referencing itself.

After that, you can now add your own directories to the end of this PATH variable.

PATH=":/home/$USER/bin:/home/$USER/git"

A screenshot showing a PATH variable with custom directories.

5. Exporting New Environment Variables

Environment variables are containers that hold session-specific values for a program or system process. It contains strings that give programs either an option or a resource that it can use while running.

For example, some programs use the “POSTGRESQL_DATABASE” environment variable when they link to an external database process:

POSTGRESQL_DATABASE="postgres://ramces:password@localhost:5432/db"

A screenshot showing a custom environment variable.

After that, you need to export the variable to your shell. This will convert it from a regular shell variable to an environment variable:

POSTGRESQL_DATABASE="postgres://ramces:password@localhost:5432/db"export POSTGRESQL_DATABASE

A screenshot showing the export command and the new environment variable.

6. Linking. bashrc with. bash_profile

The difference between. bashrc and. bash_profile is one of the biggest points of confusion for a novice Linux user. bash_profile only runs once during login while. bashrc runs whenever a new shell starts.

However, this murky division means that it is possible to mix up the two and fail to run the command that you want to run. One quick way to solve this issue is by ensuring that. bashrc runs as soon as. bash_profile loads.

Add the following line of code to the end of the. bashrc file:

if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

A terminal showing a basic if statement in bash_profile.

This small block of code will tell the shell to check if a. bashrc file exists in your home directory. If it finds one, Bash will load the file and run its commands in the current shell instance.

Frequently Asked Questions

Will my. bashrc file work with other Linux shells?

The developers of Bash designed the. bashrc file to only work with the Bash shell. However, it is important to note that most of the commands inside a. bashrc file is cross-compatible with some Unix-like shells. For example, you can copy the contents of a. bashrc file to its Korn shell counterpart:. kshrc and it will still work.

Is it possible to set my default file permissions using Bash?

Yes. To do this, you need to add the umask command at the end of your. bashrc file. This is a simple program that sets the bit mask on the permission bits that you want to set. For example, the command umask 027 is equivalent to running sudo chmod -R 750. /directory.

Image credit: Gabriel Heinzer via Unsplash. All alterations and screenshots by Ramces Red.

  • Facebook
  • Tweet


Leave a Reply

Your email address will not be published. Required fields are marked *