TrumanWong

alias

Define or display an alias.

Summary

alias [-p] [name[=value] ...]

The main purpose

  • Simplified long commands.
  • Define, modify or display one or more aliases.

Options

-p: Display all defined aliases.
name (optional): Specify the alias to be (defined, modified, displayed).
value (optional): The value of the alias.

return value

alias returns true unless the alias you want to display is undefined.

example

# Display all defined aliases
alias
alias-p

# Display defined aliases (assuming the following aliases exist in the current environment)
alias ls
alias ls grep

# Define or modify the value of the alias
alias ls='ls --color=auto'
alias ls='ls --color=never' grep='grep --color=never'

Knowledge points

The command alias set directly in the shell will become invalid after the terminal is closed or the system is restarted. How can it be permanently valid?

Use an editor to open ~/.bashrc, add alias settings to the file, such as: alias rm='rm -i', and execute source ~/.bashrc after saving, so that the alias of the command can be permanently saved.

Because what is modified is the ~/.bashrc file in the current user directory, this method is only useful to the current user. If you want it to be effective for all users, just modify the /etc/bashrc file.

Please note that the following content may differ from the system you are actually using:

Under CentOS7, this file is /etc/bash.bashrc. In addition, under CentOS7, if you look closely at the ~/.bashrc file, you will find this piece of code:

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

The meaning of this code is to load the .bash_aliases file if it exists, so you can also create a new file in the user root directory to store the command alias settings separately.

Incorrect usage

  • The alias to be displayed is undefined.
  • When you define (modify) the value of an alias, serious problems will occur because the value string has spaces but you do not enclose it in single quotes:
# For the convenience of demonstration, delete all aliases
unalias -a
# Not enclosed in single quotes
alias rm=rm -rf
# Report an error after executing the command bash: alias: -rf: not found
# At this time, when using alias to view the alias of rm, return alias rm='rm'
# More confusing example
# For the convenience of demonstration, delete all aliases
unalias -a
# Still not enclosed in single quotes
alias ls=ls --color=never
# There seems to be no error after executing the command

# Use alias to view all aliases and you will find the following results:
# alias --color=never
#alias ls='ls'
# Treat them as two groups when processing alias

Q&A

Q: What should I do if I want to display one or more aliases, but I don’t know if any of them are undefined?

A: Just execute it normally. alias will not end the execution of the remaining parameters just because there is an undefined alias.

Q: If I define alias cd='ls' ls='cd' like this, what will be the consequences?

A: Running cd will still switch directories, and running ls will still list the contents of the folder; do not define it this way.

Notice

  1. Please note when executing the script:
    • If a bash script executed using the source command executes the alias or unalias command, it may have an impact on the alias settings of the terminal environment; the alias settings of the terminal environment may also change the running results;
    • Bash scripts called through sh or directly running scripts that the current user has execution permissions are not affected by the alias of the terminal environment.
  2. To delete aliases, please see the unalias command.
  3. It is recommended that you do not set dangerous -f options for aliases of commands such as mv cp rm, such as alias rm='rm -f'.
  4. Pay attention to whether the alias conflicts with other commands.
  5. This command is a built-in bash command. For related help information, please see the help command.

Other reference links