Define or display an alias.
alias [-p] [name[=value] ...]
-p: Display all defined aliases.
name (optional): Specify the alias to be (defined, modified, displayed).
value (optional): The value of the alias.
alias returns true unless the alias you want to display is undefined.
# 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'
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.
/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.# 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: 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.