Aliases

Date Created: 04-10-2026

How to create and use aliases in the following shell environments: bash, PowerShell, and Windows Command Prompt.

Table of Contents

  1. Overview
    1. What are aliases?
    2. Why do we use them?
    3. Example Usage
  2. Bash
    1. What is ~/.bashrc?
    2. What is ~/.bash_aliases?
    3. Sourcing
    4. How to Create Aliases
  3. PowerShell
    1. What is $PROFILE?
    2. Enabling Scripts
    3. Source the $PROFILE
    4. Aliases in PowerShell
    5. Example Adding and Using PowerShell Aliases
  4. Command Prompt
    1. About Command Prompt
    2. Create an Aliases .bat or .cmd file
  5. References

Overview

What are aliases?

An alias is a short name you use instead of something longer, usually a short command that runs a longer command (in the shell), or sometimes another name for the same type or import in code.

Why do we use them?

Programmers use aliases mainly to work faster (fewer keystrokes for commands they run all day), make fewer mistakes (less retyping of long flags and paths), and keep habits consistent (the same shortcut every time, often for Git or navigation).

Example Usage

# alias definition to change directory to project directory
alias p="cd /home/user/Deskstop/project/"

# default location with new terminal
user@user-cpu:/home/$  

# enter alias
user@user-cpu:/home/$  p  

# alias changes directory to project repo.
user@user-cpu:/home/user/Desktop/project$    

Bash

Please Note: Examples are using Ubuntu 24.04 environment.

What is ~/.bashrc?

~/.bashrc is a shell startup file Bash reads for interactive shells. It’s where people put aliases, prompt tweaks, functions, and PATH changes so every new terminal session behaves the same.

On some systems, login shells read ~/.bash_profile or ~/.profile instead or first. Many distros make those files source .bashrc so everything still loads. If something only works in some terminals, that split is often why.

What is .bash_aliases?

~/.bash_aliases is a separate file that only holds alias … lines. Keeping aliases out of .bashrc makes the file easier to read and sometimes easier to sync between machines.

You use it by sourcing it from .bashrc (see below).

Sourcing

source ~/.bashrc
or
. ~/.bashrc

How to Create Aliases

  1. In your home directory, create .bashrc and .bash_aliases if the do not exist.

    # Please Note: ~/.bashrc exists by default in Ubuntu 24.04.
    sudo vim ~/.bashrc && sudo vim ~/.bash_aliases
  2. In .bash_aliases, add some aliases and save the file.

    alias d="cd /home/user/Desktop"
    alias gs="git status"
    alias v="source .venv/bin/activate"
    alias s="source ~/.bash_aliases"
  3. Source the `.bash_aliases file to load the changes.

    source ~/.bash_aliases
  4. Use your aliases.

    • Your aliases will not be available for use in your current and future bash terminal sessions.
    • Simply use them by entering the alias in the bash terminal.
    # example changing directory to Desktop using d alias
    user@user-cpu:/home/$ d
    user@user-cpu:/home/user/Desktop$

    Optional: In .bashrc, add the following snippet to always source .bash_aliases with new shell instance.

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

PowerShell

What is $PROFILE?

In PowerShell, $PROFILE is the path to your personal profile script (a .ps1 file). When an interactive session starts, PowerShell runs that script once for each new terminal and loads the profile (if profiles are enabled).

There are actually four profile paths (user, all users, current host, all hosts). The $PROFILE alone usually means current user or current host.

Enabling Scripts

If execution policy blocks your profile from running, you’ll need an appropriate policy for your machine (often RemoteSigned for CurrentUser is enough). Without that, the profile file may exist but never run.

# check execution policy
C:\Users\user> Get-ExecutionPolicy
Restricted

# change to allow script execution (if needed)
C:\Users\user> Set-ExecutionPolicy RemoteSigned

# validate execution policy change
C:\Users\user> Get-ExecutionPolicy
RemoteSigned

Source the $PROFILE

. $PROFILE  

Aliases in PowerShell

The Set-Alias command only creates an alias to a cmdlet in the current PowerShell session, not arguments. So it’s fine for something like mapping np for notepad, but not for changing directory paths.

For anything with arguments, use a PowerShell function:

# add to $PROFILE script
function d { Set-Location "$HOME$\Desktop"}

Example Adding and Using PowerShell Aliases

# open current user profile script
notepad $PROFILE

# add PowerShell function (alias)
function d { Set-Location "$HOME$\Desktop }

# save and source updated profile script or open a new PowerShell
. $PROFILE

# use your new alias
C:\Users\user> d
C:\Users\user\Desktop>

Command Prompt

**Please Note: ** Examples use Command Prompt version Microsoft Windows [Version 10.0.26200.8037]

About Command Prompt

Creating an Aliases .bat or .cmd file

  1. Create a new aliases.bat or aliases.cmd file
# create the aliases file
notepad aliases.cmd
  1. Add a simple alias and save the file
# add a few aliases
@echo off
doskey d=cd "%USERPROFILE%\Desktop"
  1. Set AutoRun for Command Prompt Registry Key
C:\Users\user> reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_SZ /d "C:\aliases.cmd" /f
The operation completed successfully.
  1. Verify Functionality

References

  1. Bash Introduction - W3Schools
  2. Bash Aliases - W3Schools
  3. PowerShell Profiles - MSDN
  4. Customizing PowerShell Environment - MSDN
  5. Doskey Reference - MSDN