---
title: "How to Use The .bashrc File in Linux"
description: "Unleash .bashrc in Linux—the power to enhance productivity and customization. Supercharge your Linux command-line experience today!"
canonical: "https://adamtheautomator.com/bashrc/"
---

# How to Use The .bashrc File in Linux

> Unleash .bashrc in Linux—the power to enhance productivity and customization. Supercharge your Linux command-line experience today!

Source: https://adamtheautomator.com/bashrc/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Use The .bashrc File in Linux](https://adamtheautomator.com/wp-content/uploads/2024/02/bashrc.jpg)

# How to Use The .bashrc File in Linux

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)16 February 20246 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Linux](/tag/linux/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [What is the Bash Run Commands (.bashrc) File in Linux?](#what-is-the-bash-run-commands-bashrc-file-in-linux)
*   [Defining Custom Aliases for Quick Command Executions](#defining-custom-aliases-for-quick-command-executions)
*   [Crafting Functions for Enhanced Productivity](#crafting-functions-for-enhanced-productivity)
*   [Personalizing Your Linux Terminal and Shell Prompt (PS1)](#personalizing-your-linux-terminal-and-shell-prompt-ps1)
*   [Launching Your Scripts: No Path? No Problem!](#launching-your-scripts-no-path-no-problem)
*   [Conclusion](#conclusion)

At some point, you definitely felt like your Linux experience could use a little extra flair. Perhaps you’ve encountered repetitive tasks or wished for a smoother command-line interface. Well, guess what? Your system holds the key: the _.bashrc_ file, quietly nestled in your home directory!

Delve into the fundamental aspects of the _~/.bashrc_ file in this tutorial to transform how you interact with your Linux system. You’re on the right track, from customizing your command prompt to automating tasks and setting up personalized shortcuts.

Say goodbye to mundane tasks and hello to a more efficient and enjoyable Linux experience!

## Prerequisites

Before you play around with the _~/.bashrc_ file, ensure you have the following in place to follow along:

*   A Linux distribution is installed on your machine – This tutorial uses [Ubuntu 20.04 LTS](https://adamtheautomator.com/install-ubuntu/), but any other distribution works.
*   A non-root user with sudo privileges.

Related:[How to Create a User on Ubuntu Linux in Multiple Ways](https://adamtheautomator.com/create-user-on-ubuntu/)

## What is the Bash Run Commands (_.bashrc)_ File in Linux?

Imagine the _~/.bashrc_ file in Linux like a secret toy box in your room. This toy box gives you and sets up your favorite toys just the way you like them as you open the box.

But because _~/.bashrc_ is a secret file, it doesn’t show when you run the [`ls`](https://www.geeksforgeeks.org/ls-command-in-linux/) command. Well, not unless you specify to show hidden ones, too, with the `-la` parameter, as shown below.

```bash
ls -la
```

Below, you can see the _**.bashrc**_ file is listed along with other ones that are also hidden.

![Listing all files, including hidden ones, in the working directory (home)](https://adamtheautomator.com/wp-content/uploads/2024/02/image-180.png)

Listing all files, including hidden ones, in the working directory (home)

## Defining Custom Aliases for Quick Command Executions

A short definition wouldn’t cut into realizing the _~/.bashrc_ file’s role in customizing your Linux experience. So, how does this file work exactly? When you crack open that _~/.bashrc_ file, one of the powerful tricks it lets you achieve is defining custom aliases for your favorite commands.

For example, instead of typing `sudo apt-get update` to update your system’s local package index, define an alias in the _.bashrc_ file that works the same way.

Related:[Ubuntu Apt Get Mastery: A Guide with Practical Examples](https://adamtheautomator.com/ubuntu-apt-get/)

> 💡 _Note: This tutorial uses the root account to run commands (not recommended) to illustrate administrative tasks, such as system configuration. But, for security reasons, ensure you’re using a non-root user with `sudo` privileges when performing similar actions on your system._

To define aliases for commands, follow these steps:

1\. Open the _`.bashrc`_ file in your home directory (`~/`) via your preferred editor, like [`nano`](https://help.ubuntu.com/community/Nano) or [`vim`](https://adamtheautomator.com/vim-for-linux/).

> 💡 _If you’re using a non-root account, prepend `sudo` when you run commands that require elevated privileges._

```bash
nano ~/.bashrc
```

2\. Next, scroll to the bottom of the file and define an `alias` (i.e., `update`) for your target command (i.e., `sudo apt-get update`), as demonstrated below.

```bash
alias update='sudo apt-get update'
```

Save the changes to the file, and close the editor.

![Defining an alias in the .bashrc file](https://adamtheautomator.com/wp-content/uploads/2024/02/image-182.png)

Defining an alias in the _.bashrc_ file

3\. Once saved, execute the following command in your terminal to [`source`](https://www.geeksforgeeks.org/source-command-in-linux-with-examples/) the _`~/.bashrc`_ file. This command produces no output to the terminal but applies your changes.

```bash
source ~/.bashrc
```

4\. Now, run the `update` alias you defined and confirm the corresponding command (`sudo apt-get update`) takes place.

```bash
update
```

The output below confirms your `update` alias works like running the `sudo apt-get update` command but with fewer keystrokes.

![Testing the newly defined alias (update)](https://adamtheautomator.com/wp-content/uploads/2024/02/image-181.png)

Testing the newly defined alias (update)

## Crafting Functions for Enhanced Productivity

Defining an alias falls short when you have intricate operations to execute repeatedly. But don’t worry; functions are here to step up to the plate for a solution where aliases might stumble.

Unlike aliases that merely serve as a shorthand for a single command, functions encompass a complex series of commands designed to perform intricate operations.

To realize how a function works, carry out the following:

1\. In the _~/.bashrc_ file, append the function below called `manage_package` (arbitrary).

When executed, this function checks if a given package is installed, and based on that information, it either updates or installs the package.

Remember, this function takes a single argument only, which is the package name you’re interested in.

```bash
manage_package() {
  # Checks if the package is installed by querying the package status with dpkg. 
  # $1 is the placeholder for the first argument passed to the function (the package name).
  # The output is redirected to /dev/null to suppress it in the terminal.
  # The if statement checks the exit status of the dpkg -s command. 
  # If the status is 0 (package is installed), 
  # proceeds to the first block, updating the package.
  if dpkg -s "$1" &> /dev/null; then
    echo "Package '$1' is already installed. Updating..."
    sudo apt-get update && sudo apt-get install --only-upgrade "$1"
  # If the package is not installed, print a message
  # and run sudo apt-get install "$1" to install the package.
  else
    echo "Package '$1' is not installed. Installing..."
    sudo apt-get update && sudo apt-get install "$1"
  fi
}
```

Related:[Understanding Bash If Else and Other Conditional Statements](https://adamtheautomator.com/bash-if-else/)

2\. With the function added, save the changes, close the editor, then `source` the _`~/.bashrc`_ file to apply the changes.

```bash
source ~/.bashrc
```

3\. Once sourced, call the function (`manage_package`), followed by the `package-name` you wish to check, update, or install in your terminal, as shown below.

This tutorial’s choice of package is NGINX, but ensure you substitute `package-name` with your preferred package name.

```bash
manage_package package-name
```

As you can see in the output, functions like this make package management a breeze by doing the heavy lifting for you. They automatically determine whether the package you want is there, so you don’t have to.

![Executing the newly defined function](https://adamtheautomator.com/wp-content/uploads/2024/02/image-183.png)

Executing the newly defined function

## Personalizing Your Linux Terminal and Shell Prompt (PS1)

Think of your Linux terminal as a personal workspace. Customizing your terminal transforms it into a more visually appealing and informative environment.

When spicing up your Linux terminal, the sky’s the limit. You can spruce your terminal up with a cool welcome message or tweak your shell prompt for that extra flair.

To personalize your Linux terminal, proceed with the following:

1\. Add the following line at the bottom of your _~/. bashrc_ file. This line prints a (greeting) message each time you start a terminal session.

```bash
echo "Welcome to your terminal, $USER!”
```

2\. Next, modify the Prompt String 1 (`PS1`) environment variable if it exists, or add the line below to your _~/.bashrc_ file. The `PS1` variable precisely controls the primary prompt string, which is the main prompt displayed (i.e., user@linux:~#) when the shell is ready to accept commands.

Related:[How To Wrangle Ubuntu Environment Variables](https://adamtheautomator.com/ubuntu-environment-variables/)

Modifying the `PS1` variable lets you change your shell prompt’s appearance, including its color, format, and display information. The following expression:

*   Makes the username (`\u`) and hostname (`\h`) appear in bright green (`32m`).
*   Causes the current working directory (`\w`) to appear in bright blue (`34m`).
*   Resets the color attributes to the default color (usually white) (`\[\033[00m\]`) for the text that follows the prompt. This reset ensures the rest of your terminal text remains unaffected.

```bash
PS1='\[\033[01;32m\]\u@\h:\[\033[01;34m\]\w\$ \[\033[00m\]'
```

3\. Afterward, save and close the file, then `source` your _`~/.bashrc`_ file to apply the changes.

```bash
source ~/.bashrc
```

Notice below that the welcome message appears immediately after sourcing, and your shell prompt changes its appearance as you defined.

![Applying the shell prompt changes](https://adamtheautomator.com/wp-content/uploads/2024/02/image-184.png)

Applying the shell prompt changes

## Launching Your Scripts: No Path? No Problem!

Enhancing your system to recognize additional directories for executable commands can streamline your workflow. Take running scripts, for example—no more wrestling with long paths, just pure convenience.

Related:[Your One and Only Linux Shell Scripting Tutorial](https://adamtheautomator.com/linux-shell-scripting-tutorial/)

To launch your scripts without memorizing each path, complete the steps below:

1\. Execute the following command to make a directory ([`mkdir`](https://www.geeksforgeeks.org/mkdir-command-in-linux-with-examples/)) called `~/scripts` (arbitrary)) dedicated to storing your scripts.

```bash
mkdir -p ~/scripts
```

2\. Next, open the _~/.bashrc_ file and add the following line to the end, which modifies the system’s command search path. This line prepends the `~/scripts` directory to your existing `PATH` variable.

This behavior guarantees the shell prioritizes your scripts over other executables with identical names elsewhere in the system’s `PATH`.

```bash
export PATH="$HOME/scripts:$PATH"
```

3\. Save and close the file, then `source` it to apply the changes without restarting the terminal.

```bash
source ~/.bashrc
```

4\. Subsequently, run each command below to create a script called `testscript` (arbitrary) in your `~/scripts` directory.

These commands have no output, but this script is made executable ([`chmod`](https://adamtheautomator.com/chmod-and-chown/)), which prints a message ([`echo`](https://adamtheautomator.com/echo-command-in-bash/)) to the terminal.

```bash
# Create a new Bash script called 'testscript' in the ~/scripts directory
echo '#!/bin/bash' > ~/scripts/testscript

# Append a line to the 'testscript' that prints "Script is running!" when executed
echo 'echo "Script is running!"' >> ~/scripts/testscript

# Make the 'testscript' executable by changing its permissions
chmod +x ~/scripts/testscript
```

5\. Finally, execute your script (`testscript`) by calling its name in the terminal.

The system executes your script from your dedicated _~/scripts_ directory regardless of your working directory in the terminal.

```bash
testscript
```

The output below confirms your script, and the system’s modified command search path works as expected.

![Executing the new script to confirm it's functioning as expected](https://adamtheautomator.com/wp-content/uploads/2024/02/image-185.png)

Executing the new script to confirm it’s functioning as expected

## Conclusion

In the _~/.bashrc_ file world, you’ve journeyed through the fundamentals of its role and how the command aliases and functions customization works. Even personalizing the feel of your Linux terminal is now within reach.

Besides the looks, you’ve unlocked the convenience of executing scripts without specifying their full paths each time by extending the `PATH` environment variable. These modifications save time and enhance the user interface, making your interaction with Linux more efficient and more enjoyable at the same time.

As you close this chapter on the _~/.bashrc_ file, remember that the true essence of Linux is its flexibility and the control it offers you. The journey continues, so why not thirst for a tailored experience by exploring ZSH’s enhancements?

Dive into the ocean of themes and plugins that ZSH and frameworks like [Oh My Zsh](https://adamtheautomator.com/install-oh-my-zsh-on-ubuntu/) provide. Watch your terminal transform into a more productive and visually engaging workspace!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fbashrc%2F&text=How%20to%20Use%20The%20.bashrc%20File%20in%20Linux)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fbashrc%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fbashrc%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/09/Practical-Linux-Unix-Tee-Commands-for-the-Linux-Admin.jpg)

### [Master Unix tee Commands for Real-World Linux Admin Tasks](/unix-tee/)

Simplify Linux output management and streamline your workflow with the “Unix tee” command. This tutorial guides you through its practical applications.

![](https://adamtheautomator.com/wp-content/uploads/2024/03/openldap-4.jpg)

### [How to Install and Configure an OpenLDAP Ubuntu Server](/openldap/)

Unlock the power of OpenLDAP on Ubuntu for centralized user authentication, seamless access control management, and enhanced directory services!

![](https://adamtheautomator.com/wp-content/uploads/2024/03/pipe-command-in-linux-1.jpg)

### [Unleashing the Power of the Pipe Command in Linux](/pipe-command-in-linux/)

Unlock the prowess of the pipe command in Linux to streamline tasks, boost productivity, and simplify complex operations effortlessly!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
