What it does
An alias replaces the first word of a command with other text. alias ll='ls -l' means: when you type ll, Bash runs ls -l.
An alias made on the command line lives only in that shell. To keep it, put the alias line in ~/.bashrc (only you) or in a file in /etc/profile.d/ (all users).
Aliases are for interactive work. In scripts, use the full commands or a function.
How to write it
alias [-p] [NAME[=VALUE] ...]
unalias [-a] NAME [NAME ...]Options 2
Most used
Nothing matches this filter.
Examples 11
$ run as a normal user# run as root (or with sudo)
1List all aliases
alias alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto'RHEL adds a few aliases for every user.
2Make a short name
alias ports='ss -tulpn' portsWorks until you close the shell.
3Keep the alias after logout
sourceloads it now; new shells read~/.bashrcby themselves.4Make a safer rm, cp and mv
alias rm='rm -i' cp='cp -i' mv='mv -i'-iasks before it removes or overwrites a file. root has these by default on RHEL.5Run the real command once, without the alias
\rm old.logA backslash (or
command rm) skips the alias.6Remove an alias
unalias portsOnly for this shell. Delete the line in
~/.bashrcto remove it for good.7Show what one alias does
alias ll alias ll='ls -l --color=auto'Good when a short name behaves in a strange way.
8Give all admins the same shortcuts
cat > /etc/profile.d/ops-aliases.sh <<'EOF' alias jctl='journalctl -xe' alias fwl='firewall-cmd --list-all' EOFEvery user gets these at the next login. Updates do not touch your file.
9Shorten a long kubectl or podman command
alias k='kubectl' alias pps='podman ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"'Typical DevOps shortcuts. Keep them in
~/.bashrc.10Check for a dangerous alias in a security review
alias | grep -E "sudo|ssh|su="An alias that changes
sudoorsshcan steal passwords. There should be none.11Remove all aliases in this shell
unalias -aUseful to test a problem without any aliases.
Nothing matches this filter.
Watch out
- Aliases are not used in scripts (non-interactive shells), unless the script turns on
shopt -s expand_aliases. - Use single quotes
'…'so variables are read when the alias runs, not when you define it. - An alias can hide a real program with the same name. Check with
type NAME.
Files
~/.bashrc- Your own aliases (read by every new shell).
/etc/profile.d/*.sh- Aliases and settings for all users.
/etc/bashrc- System-wide settings for every shell (do not edit; use profile.d).
Tips to remember
- alias = make, unalias = remove,
~/.bashrc= keep. \nameruns the real command once.