Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

alias, unalias

Make a short name for a longer command (alias), or remove it (unalias).

Level
RHCSA exam
Group
Shell and help
Needs root
No
Package
bash (built-in)
Also called
unalias

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

-p
With alias: print all aliases in a form you can use again.
-a
With unalias: remove all aliases of this shell.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 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.

  2. 2Make a short name

    alias ports='ss -tulpn'
    ports

    Works until you close the shell.

  3. 3Keep the alias after logout

    echo "alias ports='ss -tulpn'" >> ~/.bashrc
    source ~/.bashrc

    source loads it now; new shells read ~/.bashrc by themselves.

  4. 4Make a safer rm, cp and mv

    alias rm='rm -i' cp='cp -i' mv='mv -i'

    -i asks before it removes or overwrites a file. root has these by default on RHEL.

  5. 5Run the real command once, without the alias

    \rm old.log

    A backslash (or command rm) skips the alias.

  6. 6Remove an alias

    unalias ports

    Only for this shell. Delete the line in ~/.bashrc to remove it for good.

  7. 7Show what one alias does

    alias ll
    alias ll='ls -l --color=auto'

    Good when a short name behaves in a strange way.

  8. 8Give all admins the same shortcuts

    cat > /etc/profile.d/ops-aliases.sh <<'EOF'
    alias jctl='journalctl -xe'
    alias fwl='firewall-cmd --list-all'
    EOF

    Every user gets these at the next login. Updates do not touch your file.

  9. 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.

  10. 10Check for a dangerous alias in a security review

    alias | grep -E "sudo|ssh|su="

    An alias that changes sudo or ssh can steal passwords. There should be none.

  11. 11Remove all aliases in this shell

    unalias -a

    Useful to test a problem without any aliases.

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.
  • \name runs the real command once.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man alias