Aliases & Profiles
An alias is a short name for a longer command: ll for "list everything in detail", gs for git status. A
profile (or startup file) is a script your shell runs every time it starts. Together they let you shape the
terminal to your habits: put your aliases, functions, PATH changes and prompt in the profile once, and every new
window has them.
| Shell | Aliases | Startup file(s) |
|---|---|---|
| CMD | doskey macros |
none by default (an AutoRun registry value can point to a .bat) |
| PowerShell | Set-Alias, functions |
$PROFILE (Documents\PowerShell\Microsoft.PowerShell_profile.ps1) |
| bash | alias, functions |
~/.bashrc (interactive), ~/.bash_profile or ~/.profile (login) |
| zsh | alias, functions |
~/.zshrc (interactive), ~/.zprofile (login), ~/.zshenv (always) |
Windows (CMD)
CMD's aliases are doskey macros. $* stands for "all the arguments you typed after the macro":
C:\Users\ada> doskey ll=dir /a $*
C:\Users\ada> doskey gs=git status $*
C:\Users\ada> doskey np=notepad $1
C:\Users\ada> doskey /macros
ll=dir /a $*
gs=git status $*
np=notepad $1Macros only work when typed at the prompt, not inside batch files, and they disappear when the window closes. To
load them every time, save them in a file (one name=command per line) and run it at startup. A common setup is a
C:\Users\ada\cmd-init.bat containing @doskey /macrofile=C:\Users\ada\macros.txt, referenced from the registry
value HKCU\Software\Microsoft\Command Processor\AutoRun, or from the "Command line" of a Windows Terminal profile:
cmd.exe /k C:\Users\ada\cmd-init.bat.
For anything longer than one line, write a small .bat file and put its folder on your PATH
(The PATH Variable).
Windows (PowerShell)
PowerShell comes with many aliases already (cd, dir, gci, cls, echo). An alias in PowerShell can only
point to one command name without arguments; for anything with arguments, write a function.
Get-Alias gci, cd, select | Format-Table Name, Definition
Set-Alias -Name greet -Value Write-Output
greet "Hello from an alias"
function ll { Get-ChildItem -Force @args | Select-Object -ExpandProperty Name }
New-Item -ItemType Directory demo | Out-Null
New-Item demo/b.txt, demo/a.txt | Out-Null
ll demo
Get-Command ll | Select-Object -ExpandProperty CommandType
Name Definition
---- ----------
gci Get-ChildItem
cd Set-Location
select Select-Object
Hello from an alias
a.txt
b.txt
Function
@args passes along whatever arguments you gave ll. Put aliases and functions like these in your profile:
PS C:\Users\ada> $PROFILE
C:\Users\ada\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
PS C:\Users\ada> Test-Path $PROFILE
False
PS C:\Users\ada> New-Item -ItemType File -Path $PROFILE -Force
PS C:\Users\ada> notepad $PROFILEA typical profile:
# Microsoft.PowerShell_profile.ps1
Set-Alias -Name np -Value notepad
function gs { git status @args }
function .. { Set-Location .. }
$env:EDITOR = "code --wait"
Set-PSReadLineOption -PredictionSource HistoryReload it without restarting with . $PROFILE. If Windows refuses to run it ("running scripts is disabled on this
system"), allow local scripts once with Set-ExecutionPolicy -Scope CurrentUser RemoteSigned. Windows PowerShell
5.1 uses a separate profile under Documents\WindowsPowerShell\.
Linux & macOS (bash/zsh)
alias ll='ls -1A'
alias gs='git status --short'
alias ll
mkdir demo && touch demo/b.txt demo/.hidden demo/a.txt
shopt -s expand_aliases
ll demo
mkcd() { mkdir -p "$1" && cd "$1"; }
mkcd projects/site && pwd
type ll mkcd | head -n 2alias ll='ls -1A'
.hidden
a.txt
b.txt
/home/learner/projects/site
ll is aliased to `ls -1A'
mkcd is a function
- In an interactive terminal aliases just work. Scripts ignore aliases in bash unless you
shopt -s expand_aliases, so use functions in scripts. aliasalone lists all aliases;unalias llremoves one;\lsorcommand lsbypasses an alias for one call.- A function like
mkcdcan use arguments ($1) anywhere, run several commands and change the current folder.
Where to put them:
bash on Linux: terminal window opens -> ~/.bashrc (put aliases, functions, prompt here)
login (ssh, console) -> ~/.bash_profile or ~/.profile (usually just sources ~/.bashrc)
zsh on macOS: login shell -> ~/.zprofile (PATH, e.g. Homebrew's shellenv line)
every interactive one -> ~/.zshrc (aliases, functions, prompt, completion)On macOS every new Terminal window starts a login shell, so both ~/.zprofile and ~/.zshrc run. After editing,
reload with source ~/.bashrc or source ~/.zshrc (or open a new window).
zsh extras
zsh expands aliases in scripts too, and adds global aliases (-g, expanded anywhere on the line) and suffix
aliases (-s, open a file by typing its name):
alias ll='ls -1A'
alias -g C='| wc -l'
print -l one two three > words.txt
cat words.txt C
alias -s txt=cat
words.txt
alias -L ll3
one
two
three
alias ll='ls -1A'
Use global aliases sparingly: C is now replaced in every command, even where you meant a literal C.
Comparison
| Task | CMD | PowerShell | bash / zsh |
|---|---|---|---|
| Define a shortcut | doskey ll=dir /a $* |
function ll { Get-ChildItem -Force @args } |
alias ll='ls -la' |
| Simple rename | doskey np=notepad $* |
Set-Alias np notepad |
alias np=nano |
| List aliases | doskey /macros |
Get-Alias |
alias |
| Remove | doskey ll= |
Remove-Item alias:np |
unalias ll |
| Startup file | AutoRun / cmd /k init.bat |
$PROFILE |
~/.bashrc, ~/.zshrc |
| Reload it | rerun the .bat | . $PROFILE |
source ~/.bashrc |
Learn more: bashrc & Shell Config, PowerShell Functions, History, Completion & Shortcuts.
Test yourself
Why can't Set-Alias ll Get-ChildItem -Force work in PowerShell?
- ll is a reserved word
- PowerShell aliases point to a command name only, without arguments; use a function
- Aliases must be upper case
- Set-Alias only works in the profile
Which file does an interactive zsh read on macOS for aliases and the prompt?
- ~/.bashrc
- ~/.zshrc
- $PROFILE
- /etc/paths
Why does an alias defined in a bash script often not work later in that same script?
- Aliases need sudo
- bash doesn't expand aliases in non-interactive shells unless expand_aliases is set
- Scripts can't contain the word alias
- Aliases only work in zsh