Linux

Configuring .bashrc: A Complete Guide to Terminal Customization

This guide thoroughly explains what the .bashrc file is and how to edit it for task automation, changing the command line appearance, and setting environment variables. You'll learn to create useful aliases and customize your terminal.

Updated at February 16, 2026
15-30 minutes
Easy
FixPedia Team
Применимо к:Bash 4.0+Ubuntu 20.04+Debian 11+CentOS 8+Fedora 35+

Introduction / Why It's Needed

The .bashrc file (bash run commands) is a configuration script that automatically runs every time you start a new interactive Bash terminal window. Configuring this file allows you to:

  • Automate routine commands using aliases.
  • Customize the command line appearance (the PS1 prompt, colors).
  • Set global environment variables (PATH, EDITOR).
  • Load additional functions and scripts.

This guide will help you turn your standard terminal into a powerful and convenient tool tailored to your tasks.

Requirements / Preparation

Before you begin, ensure that:

  1. You are using the Bash shell (usually the default). You can verify this with the command echo $SHELL. If the path ends with /bash, you're good.
  2. You have access to your home directory (~).
  3. A text editor is installed (nano is recommended for beginners, vim for advanced users).
  4. You understand basic Linux commands (ls, cd, cat).

Step-by-Step Guide

Step 1: Find and Open the .bashrc File

The .bashrc file is located in your home directory. It may be hidden (starts with a dot).

# Navigate to the home directory
cd ~

# Check if the file exists
ls -la .bashrc

# If the file doesn't exist, create it (it often already exists)
touch .bashrc

Open the file in an editor. For beginners, nano is simpler:

nano ~/.bashrc

For experienced users, vim or vim ~/.bashrc will work.

💡 Tip: Before editing, make a backup: cp ~/.bashrc ~/.bashrc.backup.

Step 2: Add a Simple Alias

An alias is a shortcut for a long command. Add the following lines to the end of your .bashrc file:

# Update package list and install (for Debian/Ubuntu)
alias update='sudo apt update && sudo apt upgrade -y'

# Short file list with details and colors
alias ll='ls -la --color=auto'

# Quick move up one directory level
alias ..='cd ..'

# Clear screen with an extra message (example)
alias cls='clear && echo "Terminal cleared"'

Save the file (Ctrl+O in nano, :wq in vim) and close the editor.

Step 3: Customize the Command Prompt (PS1)

The PS1 variable defines how your input prompt looks. The default can be dull.

Add a line to your .bashrc to customize it:

# Example: user@host:/current/folder$
PS1='\[\e[0;32m\]\u@\h:\w\$\[\e[0m\] '

# Or with time and a basic color
# PS1='[\t] \u@\h:\w\$ '

Decoding the codes:

  • \u — username.
  • \h — hostname (up to the first dot).
  • \w — full path to the current directory, with ~ for home.
  • \$ — shows # for root, $ for a regular user.
  • \[\e[...m\] — control sequences for colors (green 0;32, reset 0m).

⚠️ Important: Color codes must be wrapped in \[ and \], otherwise commands will "eat" characters in your history.

Step 4: Add Custom Functions

For complex command sequences, use functions. They are more powerful than aliases because they can accept arguments.

Add to your .bashrc:

# Creates a folder and immediately changes into it
mkcd() {
    mkdir -p "$1" && cd "$1"
}

# Search for files by name (case-insensitive)
findfile() {
    find . -type f -iname "*$1*" 2>/dev/null
}

# Quick view of disk usage in the current folder
diskspace() {
    du -sh . 2>/dev/null || echo "Access error"
}

Now the command mkcd new_folder will create and open the folder new_folder.

Step 5: Export Environment Variables

Environment variables are available to all programs launched from the shell.

# Set the default editor (for git, crontab -e, etc.)
export EDITOR=nano
# Or for Vim: export EDITOR=vim

# Add custom binaries to PATH
export PATH="$HOME/.local/bin:$PATH"

# Set language for some utilities (e.g., for date output)
export LC_TIME="ru_RU.UTF-8"

Step 6: Apply the Changes

After saving .bashrc, the changes won't take effect in already open terminal windows. There are two ways:

  1. For the current window: run the command
    source ~/.bashrc
    

    or its equivalent
    . ~/.bashrc
    
  2. For all future windows: simply close and reopen your terminal.

Test your aliases (ll, ..) and the new command prompt.

Verification

Ensure everything works correctly:

  1. Check aliases: run alias. The list should include the ones you added.
  2. Check the prompt: it should match your PS1 settings (color, format).
  3. Check the function: try mkcd test_folder — a folder named test_folder should be created and you should navigate into it.
  4. Check variables: run echo $EDITOR — it should output nano (or whatever you specified).

If something isn't working, open .bashrc and check the syntax (especially quotes and spaces).

Troubleshooting

Problem: Terminal fails to start after editing .bashrc

Cause: A syntax error in the file (e.g., an unclosed quote). Solution:

  1. Press Ctrl+Alt+F3 (or another F-key) to switch to a text console.
  2. Log in with your username.
  3. Edit the file to fix the error: nano ~/.bashrc.
  4. Return to the graphical interface (Ctrl+Alt+F1 or F2).

Problem: Colors in PS1 display as garbled characters

Cause: Incorrect use of control sequences or missing \[ \] wrappers. Solution: Ensure all ANSI color codes are wrapped in \[ and \]. Example of correct formatting: PS1='\[\e[1;34m\]\u@\h:\w\$\[\e[0m\] '.

Problem: Aliases don't work in sudo commands

Cause: sudo by default resets environment variables, including aliases. Solution: Use sudo with the -E flag (preserve environment) or create an alias for sudo (e.g., alias s='sudo'), but this is less secure. It's better to explicitly write the full command after sudo.

Problem: .bashrc changes don't apply on SSH connection

Cause: On SSH login, .bash_profile or .profile is read instead of .bashrc (depends on the distribution). Solution: In your ~/.bash_profile (or ~/.profile) file, add the line:

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

This ensures .bashrc is executed on login.

F.A.Q.

Is it safe to edit .bashrc? What should I do if the terminal breaks?
What is the difference between .bashrc and .bash_profile?
How to apply changes in .bashrc without restarting?
Why don't my aliases work in scripts?

Hints

Find and open the .bashrc file
Add a simple alias
Customize the command prompt (PS1)
Add custom functions
Export environment variables
Apply changes
FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community