Back to Explore
GuideVibeFix editorialUpdated Nov 30, 2026

Shell Aliases from Zero: Short Names for Commands You Type All Day, and the Script Trap to Dodge

Commands you type dozens of times a day deserve a short name. This guide starts from zero on shell aliases: defining them with alias, small ones like ll and now, the trap where aliases do not expand in scripts, why aliases cannot take arguments and functions must take over, and how double quotes freeze the time. Every step was run for real on GNU bash 5.2.21.

Real photograph of hands typing on a laptop keyboard with code on the screen

People who write code type the same batch of commands in the terminal dozens or hundreds of times a day. ls -l to see file details, git status to see what changed, cd back and forth into project directories. None of these commands is hard. The hard part is typing them over and over, all day, and mistyping one letter. An alias is a short name for a command you use all the time: you type two letters and the shell expands them into the full thing for you. This guide starts from zero on shell aliases: what they are, how to define them, three you can use right away, and the three real traps beginners fall into. Every step was run for real on GNU bash 5.2.21, and the transcript is included at the end; every line of output quoted here matches that record word for word.

First, what an alias is. The Bash manual puts it plainly: an alias lets one string be substituted for the first word of a simple command. You type ll, the shell checks an alias table before running anything, finds that ll stands for ls -l, replaces ll with ls -l, and then runs it. The key phrase is "the first word": only the word at the start of the line gets replaced. Everything after it is left alone. ll /tmp expands to ls -l /tmp, and /tmp is still /tmp, untouched. An alias is not a new command. It is a text substitution, and what runs after the substitution is the original command.

1. What an alias is: a short name for a command

There is only one way to define an alias: alias name='value'. The name is the short word you want to type, and the value is the full command it expands to. The demo defined two: alias ll='ls -l' and alias now='date +%H:%M:%S'. To check whether a definition took, use type: type ll prints ll is aliased to `ls -l', and type now prints now is aliased to `date +%H:%M:%S'. type is the general-purpose way to ask "what is this word, really?" — alias, function, builtin, or executable file, it tells you. Learn it alongside aliases and you will save yourself a lot of debugging later.

To see every alias in the current shell, run alias with no arguments. It lists them one per line, in the same form you defined them: alias lsa='ls -a'. At the start of the demo, alias printed nothing at all — no alias had been defined yet. At the end it listed three: lsa, now, and nowdq. The same command does two jobs: with no arguments it lists, with name=value it defines. Do not mix the two up.

Remove an alias with unalias followed by its name: unalias ll. After that, type ll prints type: ll: not found with exit code 1 — the name is no longer an alias, and the shell cannot find a command called ll either. When you unalias, name the alias itself, not what it expands to. unalias ls -l is wrong: the shell looks for an alias called ls and another called -l, and finds neither.

Alias names have rules. The Bash manual says a name may not contain a slash, a dollar sign, a backquote, an equals sign, or any shell metacharacter. In plain language: stick to plain letters, digits, and underscores. ll, gs, and now are all fine; names with spaces, equals signs, or slashes are not. And never give an alias the same name as a system command as a joke: alias ls='rm' means anyone — including future you — who types ls expecting a file listing gets a deletion instead.

2. Three aliases you can use right away

The first is ll: alias ll='ls -l'. ls -l is the most common way to see file details — permissions, sizes, times, all in one place — and ll is two letters instead of five. In the demo, ll printed total 0 as its first line. The working directory was empty at that point, so the listing was empty too. Do not be alarmed by total 0: it just means "there are no files in this directory right now", not that the command failed.

The second is now: alias now='date +%H:%M:%S'. date on its own prints a long string of year, month, day, and weekday; +%H:%M:%S takes just the hours, minutes, and seconds, so now tells you the time at a glance. In the demo, now was run twice in a row and printed the current time both times. The alias does something very simple here: it hides a long list of options behind one easy word.

The third is the one programmers use most, gs: alias gs='git status'. git status is one of the most-typed git commands there is; shortened to gs, one tap after editing tells you which files changed. The demo did not run gs, because the demo directory was not a git repository and it would only have errored; but the definition works exactly like ll and now, so you can define it as shown and use it. One more thing: a definition can carry options with it. alias gs='git status --short --branch' passes those two options along every time it expands.

Aliases can also shorten the "walking" between directories: alias ..='cd ..' goes up one level, and alias ...='cd ../..' goes up two. Project directories are usually nested deep — src/components/button, four or five levels, is everyday stuff — and getting back to the project root with cd .. takes several rounds of typing, while ... does it in one word. Add alias c='clear' and one tap cleans up a messy screen. Each of these is tiny, too small to be worth a function, but the keystrokes they save across a day add up to more than any single big alias. The value of aliases is not in any one of them being clever; it is in shrinking the small moves you repeat dozens of times a day.

How an alias works: you type ll, the shell checks the alias table, finds ll, replaces it with ls -l, and then runs it

3. The first real trap: aliases do not expand in scripts

This is the first real trap in this guide, and the one beginners fall into most often. Aliases work fine in an interactive terminal and then silently stop working inside a script. In the demo, alias ll='ls -l' was defined and ll was typed right after it in the same script. The output was ll: command not found, exit code 127. The alias had just been defined, and the shell still said it could not find the command. The problem is not the definition; it is the expansion. The Bash manual states it plainly: aliases are not expanded when the shell is not interactive, unless the expand_aliases option is set with shopt.

The fix is one line: shopt -s expand_aliases. Put it before the alias definitions, and from that line on, aliases expand inside scripts too. In the demo, ll worked immediately after that option was turned on, printing total 0 with exit code 0. Same script, same alias, same ll — the only difference was the switch. Remember its name: expand_aliases, expand aliases. shopt is the command that manages switches like this one; -s turns one on and -u turns it off.

Why is it designed this way? Because scripts are meant to be repeatable and portable: run the same script on another machine and the alias table may be completely different, so a script that depends on aliases breaks in a new environment. An interactive terminal is your own space, where aliases can be whatever you like; a script is something other people and other machines will run, so it should not depend on aliases. The manual adds an even blunter piece of advice: for almost every purpose, shell functions are preferable to aliases. The second trap will show you how much weight that sentence carries.

There is a related detail with the same root cause: an alias is expanded when the shell reads a command, not when it executes it. So a definition and its first use must be on different lines: if they share a line, or sit inside the same compound command, the use is read before the alias exists and the expansion misses it. The demo script deliberately puts definitions and uses on separate lines for exactly this reason. Definitions at the top of the script, one per line, uses on their own lines after that — that is the form that does not bite.

4. The second real trap: aliases cannot take arguments

The most common misuse of aliases is trying to make one accept arguments like a function. The demo defined alias sayhi='echo "hi $1, welcome"', hoping sayhi Lee would print "hi Lee, welcome". What actually came out was hi , welcome Lee — Lee landed at the end of the line and the $1 slot was empty. An alias is only text substitution: sayhi Lee expands to echo "hi $1, welcome" Lee, and $1 is empty here (the script has no positional parameters), so Lee is just appended at the end unchanged. It looks like it works, but that is an accident: sayhi Lee Wang prints hi , welcome Lee Wang, and $1 stays empty forever.

The manual is absolute about this: there is no mechanism for using arguments in the replacement text, as in csh. If arguments are needed, use a shell function instead. The fix is to turn the alias into a function: sayhi() { echo "hi $1, welcome"; }, and then sayhi Lee prints hi Lee, welcome. Inside a function, $1 really is the first argument, and Lee lands in the middle of the greeting where it belongs. The demo unaliased sayhi before defining a function with the same name — without that step, the alias would still win the replacement race and the function would never get a turn.

This draws a clean line: fixed phrases belong in aliases, things that take arguments belong in functions. ll, now, and gs are aliases because what you type is the same every time. mkcd belongs in a function because it takes a different directory name each time: mkcd() { mkdir -p "$1" && cd "$1"; }. The test is simple: does the short command you are dreaming up have a part that changes every time? If yes, write a function. If no, write an alias.

5. The third real trap: double quotes freeze the time

Quoting is where alias definitions most often go wrong. The demo defined alias nowdq="echo The time is $(date +%T)" with double quotes. Double quotes mean "run the $ and the command substitution right now": when that definition line ran, $(date +%T) was evaluated on the spot and baked into the alias value. After that, no matter when you type nowdq, it prints the time from the moment of definition. In the demo the two calls had a two-second sleep between them, and both printed exactly the same The time is 22:05:52 — the time was frozen, and no amount of waiting changes it. The clearest evidence is in the final alias listing: the nowdq line shows as alias nowdq='echo The time is 22:05:52', with the time written into the alias for good. (That 22:05:52 is the time of this particular run; yours will be different, but the point is that the two calls are identical.)

The fix is to swap the double quotes for single quotes: alias now='date +%H:%M:%S'. Inside single quotes, everything is kept literally; $(...) and $ are not evaluated early, and only run when the alias expands and the command actually executes. The now in the demo is defined with single quotes, so every call runs date again and shows the current time. The rule of thumb: define aliases with single quotes by default. Double quotes are not forbidden, but you must know what they do: the $ and backquotes and $() inside them are evaluated at definition time, and the result is frozen in place.

This trap has a second form: $HOME inside an alias value. With double quotes, alias homedir="echo $HOME" replaces $HOME with the home directory path at definition time and freezes it there. With single quotes, alias homedir='echo $HOME' reads $HOME fresh on every call. Home directories rarely change, so this version usually stays hidden; but switch users with su, or change HOME, and the double-quoted version prints the old address. Single quotes are the safer default.

6. Making aliases survive a restart: the startup file

An alias typed straight into the terminal lives only in that shell. Close the terminal and open a new one, and it is gone — redefining it every time is unbearable. To make aliases stick, write them into the shell's startup file. For bash that file is ~/.bashrc: every time a new interactive terminal opens, bash reads it, and the alias definitions in it take effect automatically.

The demo did not touch the real ~/.bashrc — that is the genuine article, and draft work does not touch it. Instead it used a practice file called myrc containing a single line, alias lsa='ls -a', then loaded it with source myrc and checked with type lsa, which printed lsa is aliased to `ls -a'. source means "have the current shell run this file directly", the same as typing the definition in the terminal. Even in a fresh child shell, sourcing myrc again made type lsa recognise it — the startup file works on exactly this mechanism: each new terminal sources it once, and the aliases inside are all there.

One more thing that is easy to miss: an alias lives only in the shell that defined it and is not passed to child shells. The myrc test is ready-made proof: after sourcing myrc in the main script, the child shell had to source myrc again before type lsa would recognise it — the parent's alias table did not travel along. Functions have an exception here: export -f can pass a function to a child shell, but there is no equivalent for aliases. So do not count on "define once, use everywhere": every new terminal and every child shell gets its aliases from the startup file. That is one more reason to keep them in a file: loading from a file is more reliable than remembering to define them by hand.

The right order for adding aliases to ~/.bashrc is: try the definition in the current terminal first, run it a few times to confirm it works, then write it into the file, then open a new terminal to verify. Never do it the other way round: writing a pile of untested aliases straight into the file leaves you with no idea which line is wrong. After adding, open a new terminal, run alias, and look for the line you just added — if it is in the list, it took effect. One more small habit: group aliases by purpose with a comment line above each group, like "# git shortcuts", so that three months later you can see at a glance what each group is for.

Once aliases pile up, ~/.bashrc gets long and hard to scan. A tidy fix is to put them in their own file, ~/.bash_aliases, and add one line at the end of ~/.bashrc: source ~/.bash_aliases. The effect is exactly the same as writing them into ~/.bashrc directly, except the startup file and the alias list live in separate homes: edit aliases in ~/.bash_aliases only, edit startup behaviour in ~/.bashrc only, and they never get in each other's way. Many Linux distributions ship a default ~/.bashrc that already contains this loading step, so creating the file is enough; on systems without it, adding one source line by hand does the same job. The demo used a practice version of the same idea: myrc is a miniature alias list file, and sourcing it turns it on.

7. Alias, function, or script: how to choose

By now all three tools are on the table — alias, function, and script — and they get mixed up a lot, but their jobs are distinct. An alias is the lightest: pure text substitution, good for shortening a fixed command. A function is the middle weight: it takes arguments, holds several lines, and runs in the current shell, good for shortcuts where something changes each time. A script is the heaviest: its own file, callable by anyone from any shell, good for operations worth sharing, reusing, and putting under version control.

An example makes it clear. ll is an alias, because it is the same every time. mkcd is a function, because it takes a new directory name each time. And an operation like "back up the project directory with today's date" belongs in a script file, because it has several steps, teammates will use it, and it belongs in git. Choosing wrong will not bring the sky down, but choosing right saves effort: stuffing arguments into an alias hits the trap from section four, and writing a one-line shortcut as a script is just clumsy.

There is also a safety line: never give a dangerous command a short alias. alias rm='rm -rf' is the worst example there is: it drops the price of deletion to two letters, and one mistype can wipe out a whole day's work. If you want safer deletion, add a confirmation switch instead (alias rm='rm -i'), so it asks before deleting, or simply leave rm alone. Likewise, never use an alias to "cover" a common command's name: alias grep='rm' is a joke you should not write even on your own machine — you never know when a script will run grep inside your shell.

The manual adds one small rule: if the last character of an alias value is a blank, the shell checks the next word after the alias for alias expansion too. This is called chained expansion — alias sudo='sudo ' lets aliases after sudo expand as well. You will rarely need it, but it is worth knowing: an alias does not only replace the first word, because a trailing blank lets it replace one more.

And here is an escape hatch: if an alias has taken over a real command's name and you want the real command back just once, you do not have to delete the alias first. Two ways: put a backslash in front, \ll, and the alias is skipped — the word is looked up as a plain command; or use the builtin command, as in command ll, which also bypasses aliases. The demo verified both: \ll and command ll each reported ll: command not found with exit code 127 — because ll was never a real command, only an alias, so with the alias bypassed there is nothing to find. Turn it around, though: if ll were a real command, both forms would have run the real one. That is most useful when you suspect an alias is causing trouble: run the plain command once and the alias version once, compare the two, and the culprit shows itself.

8. Checklist: follow this the next time you set up aliases

First, decide whether the shortcut is the same every time: fixed means alias, changing means function. Second, define it with single quotes: alias name='value', and keep $ and $() out of double quotes so nothing gets evaluated early. Third, put definitions and uses on separate lines, and in scripts turn on shopt -s expand_aliases before using any alias. Fourth, test it in the current terminal, then write it into ~/.bashrc, then open a new terminal and check the alias list to confirm it took effect. Fifth, remove an alias with unalias and a function with unset -f — do not mix the two up. Sixth, never give a dangerous command a short alias; rm gets -i at most, never -rf.

Test record: every step above was run for real on GNU bash 5.2.21, and the full output is in demo278_alias.sh and demo278_alias_transcript.txt (43 lines). Aliases are not hard. The hard part is remembering their three boundaries: they do not expand in scripts by default, they cannot take arguments, and double quotes freeze their values. Keep those boundaries in mind and aliases become the most effort-saving tool in the terminal: a few hundred fewer keystrokes a day, and over a year that is time enough to write several more small projects.

Views 0Comments 0

Comments (0)

ME
0/1000
Loading comments...

Sources

Browse projectsPublish your project

Related articles

Real photograph of a wooden desk with a white alarm clock beside a calendar page (illustrative photo)
Guide
crontab From Zero: Jobs That Just Run on Time, Five Fields Explained Once

cron is the scheduling daemon that has run on Linux for decades: write a crontab file saying 'run this command at this time', and it handles the rest. This guide runs everything for real on Debian cron 3.0pl1-162 — installing a job, waiting one minute for the trigger, verifying the log, removing the job — plus special strings, five classic mistakes, and a real 3 a.m. log-archiving job.

Developer WorkflowAutomationTool Tips
Real photograph of a server room patch panel filled with blue network cables (illustrative photo)
Guide
ping From Zero: One Packet Tells You If It Is Alive — the Exit Code Matters More Than the Output

ping does exactly one thing: send an ICMP echo request, wait for the echo reply, and report the round-trip time and packet loss. This guide runs every command for real on iputils ping 20240117 — packet count, interval, quiet mode, packet size, timestamps, IPv4 vs IPv6, the three failure modes, exit codes 0/1/2 — with output pasted verbatim.

Developer WorkflowTool TipsDebugging
Real photograph of scissors cutting a white sheet of paper on a white background (illustrative photo)
Guide
cut From Zero: Slicing Columns Out of Lines

cut does one thing: split each line into fields on a delimiter and output the fields you ask for. This guide runs every command for real on GNU coreutils 9.4 — field lists and ranges, CSV and output delimiters, character slicing, --complement, the -s rule for lines without a delimiter, log-column statistics, and the quoted-comma limit — with the output pasted verbatim.

Developer WorkflowTool TipsAI Coding