How to create an alias in the terminal — and keep it forever

Open a terminal on Ubuntu and type ll. You get a detailed file listing. Now try to find the program called ll:
type ll
ll is aliased to `ls -alF'
There's no such program. It's an alias — a short name the terminal expands into a longer command. Ubuntu put it in your settings ahead of time. You've been using an alias without knowing the word. Let's make your own.
What an alias is and why
An alias is your short name for a command. You type gs — the terminal runs git status.
Why bother: commands you type twenty times a day stop eating your time. And you make fewer typos. Especially handy for long commands your AI assistant tells you to run again and again.
Step by step
-
Find out which shell you have.
echo $SHELL/bin/zshmeans zsh (the Mac default since 2019)./bin/bashmeans bash (almost every Linux). This decides which file you write to. -
Check that the name is free.
type gsIf it says
not found, take it. If there's already a program, pick another name. This isn't paranoia:gsis Ghostscript, and if you have it installed, your alias will shadow it. -
Try the alias right now.
alias gs='git status'No spaces around
=. Put the command in single quotes. Typegs— it works. But only in this window: close the terminal and the alias is gone. -
Save it for good. Append a line to your settings file:
echo "alias gs='git status'" >> ~/.zshrcFor bash, replace
~/.zshrcwith~/.bashrc. Note the two>>signs: they append to the end. A single>wipes the whole file. It's a hidden file with a dot at the start — more on those in what is a dotfile. -
Load the changes.
source ~/.zshrcWhy
sourceand not running the file is explained in source vs running a script. Or just open a new terminal window. -
Check it.
type gsshould now saygs is an alias for git status.
When you need a function, not an alias
An alias just substitutes text. It can only take an argument at the end. If the value needs to go in the middle, or twice, you need a function.
Example: create a folder and step into it right away.
mkcd() { mkdir -p "$1" && cd "$1"; }
Put it in the same .zshrc or .bashrc. Now mkcd new-app does both steps. $1 is the first word after the command. The quotes around it protect you from folders with spaces — why that matters is in the article on scripts that break on spaces.
What you get
Five aliases that pay off on day one:
alias gs='git status'
alias gd='git diff'
alias dev='npm run dev'
alias ..='cd ..'
alias ports='lsof -iTCP -sTCP:LISTEN -P -n'
The last one shows which programs are listening on ports. A lifesaver when the dev server says the port is already in use and you can't remember by whom.
The alias command with no arguments lists all your aliases. To drop one temporarily — unalias gs. To bypass an alias once, add a backslash: \ls runs the real ls.
Which commands to shorten
Don't guess — ask your history. This line shows the ten commands you type most:
history | awk '{print $2}' | sort | uniq -c | sort -rn | head
In zsh, history only shows the last few lines by default — use history 1 instead to count the whole history. The top of that list is your alias shortlist. Usually it's git, cd, npm and ls — hence the five above.
One rule: don't name an alias after an existing command if you change what it means. alias rm='rm -i' is harmless. But an alias that swaps cp for something else will one day fire in a window you forgot about — and you won't understand what happened.
Why doesn't my alias work in a script?
A script runs in a separate shell that doesn't read your .zshrc. And bash doesn't expand aliases in scripts at all. Write full commands in scripts.
Why doesn't my AI agent see my aliases?
Agents often run commands in their own shell, without your settings. When you ask an agent to run something, give the full command, not your shortcut.
What about Windows?
PowerShell has Set-Alias, but it can only rename a command, without arguments. For git status people write a function in their $PROFILE file.
Short story-lessons, an agent simulator and daily practice — in our mobile app. Free.





