alias / unalias
| Since: | 全Linux | |
|---|---|---|
| macOS(2001 Cheetah) | ||
| Bash 1.0(1989) |
alias assigns a short name (alias) to a command. You can invoke long commands or frequently used options by a shorter name. Use unalias to remove an alias. Aliases are only active for the current shell session; to make them permanent, add them to your .bashrc or .zshrc file.
Syntax
Defines an alias. There must be no spaces around the =.
alias name='command'
Lists all currently defined aliases.
alias
Shows the definition of a specific alias.
alias name
Removes an alias.
unalias name
Removes all aliases at once.
unalias -a
Alias Reference (Common Examples)
| Alias Definition | Description |
|---|---|
| alias ll='ls -la' | Lists files in detail, including hidden files. |
| alias la='ls -A' | Lists files including hidden ones, except . and ... |
| alias ..='cd ..' | Moves up one directory level. |
| alias ...='cd ../..' | Moves up two directory levels. |
| alias grep='grep --color=auto' | Colorizes grep match output. |
| alias rm='rm -i' | Prompts for confirmation before deleting (prevents accidental deletion). |
| alias cp='cp -i' | Prompts before overwriting a file during copy. |
| alias mv='mv -i' | Prompts before overwriting a file during move. |
| alias h='history' | Displays command history. |
| alias c='clear' | Clears the terminal screen. |
Sample Code
A basic example of defining and using an alias. The long command ls -la is given the shorter name ll.
alias ll='ls -la' ll nerv_report.txt -rw-r--r-- 1 rei nerv 1024 Apr 9 21:00 nerv_report.txt
Running alias with no arguments lists all currently defined aliases.
alias alias ll='ls -la' alias grep='grep --color=auto' alias rm='rm -i'
Passing a specific alias name shows only that alias's definition.
alias ll alias ll='ls -la'
Use unalias to remove an alias. After removal, the name is no longer recognized.
unalias ll ll nerv_report.txt -bash: ll: command not found
Defining several aliases at once. The following sets up useful aliases for NERV operations.
alias report='cat /var/log/nerv/angel_detection.log' alias backup='cp eva_unit01.log eva_unit01.log.bak' alias status='ps aux | grep eva' alias nerv_home='cd /home/rei/nerv' alias ll='ls -la --color=auto'
Persisting aliases in .bashrc
# Aliases to add to ~/.bashrc # File navigation alias ll='ls -la --color=auto' alias la='ls -A' alias ..='cd ..' alias ...='cd ../..' # Safety guards (prevent accidental overwrite/delete) alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # NERV operations alias report='cat /var/log/nerv/angel_detection.log' alias eva_log='tail -f /var/log/nerv/eva_unit01.log' alias nerv_home='cd /home/rei/nerv'
After saving the file, reload it in the current session with source.
source ~/.bashrc
Aliases cannot take arguments. Use a function when you need argument handling.
NG: trying to pass an argument to an alias
# Alias cannot handle arguments alias greet='echo "Hello, $1"' # $1 is not expanded as an argument
$1 is expanded as a literal string when the alias is defined, so it cannot work as a runtime argument.
OK: nerv_report.sh (use a function when arguments are needed)
#!/bin/bash
nerv_report() {
local target="$1"
echo "=== NERV Report: $target ==="
cat "/var/log/nerv/${target}.log"
}
nerv_report "angel_detection"
nerv_report "eva_unit01"
Overview
Aliases are only active in the current shell process. They are not inherited by subshells (such as inside scripts) by default. To use aliases inside a script, enable shopt -s expand_aliases (bash). That said, using aliases in scripts is uncommon — functions are generally preferred for their portability and readability.
You can shadow an existing command name (ls, grep, etc.) with an alias to automatically prepend options. To bypass the alias and call the original command, prefix the name with a backslash (\ls) or use command ls.
zsh supports alias -g (global aliases), which expand anywhere in a command line — not just at the beginning. For example, alias -g L='| less' lets you write cat file L. This feature is not available in bash.
Common Mistakes
Common Mistake 1: Spaces around the = sign
Adding spaces around = in an alias definition causes an error.
alias ll = 'ls -la' alias ll='ls -la' -bash: alias: ll: not found -bash: alias: =: not found -bash: alias: ls -la: not found
The shell treats ll, =, and 'ls -la' as separate arguments. Write the definition with no spaces around =.
alias ll='ls -la'
Common Mistake 2: Single quotes vs. double quotes change when variables are expanded
When you define an alias with single quotes, variable expansion happens when the alias is executed. With double quotes, expansion happens when the alias is defined.
export NERV_DIR="/home/rei/nerv" alias go_nerv_single='cd $NERV_DIR' alias go_nerv_double="cd $NERV_DIR" export NERV_DIR="/home/shinji/nerv" go_nerv_single pwd /home/shinji/nerv
With single quotes, $NERV_DIR is evaluated at execution time, so updating the variable after defining the alias takes effect immediately.
go_nerv_double pwd /home/rei/nerv
With double quotes, $NERV_DIR is fixed to the value at definition time (/home/rei/nerv). Changing the variable later has no effect. Use single quotes to pick up the current variable value at runtime; use double quotes only when you want to hard-code the value at definition time.
If you find any errors or copyright issues, please contact us.