Define or display aliases.
alias [-p] [name[=value] ...]
-p: Display all defined aliases.
name (optional): Specify the alias to define, modify, or display.
value (optional): The value of the alias.
alias returns true unless an alias you try to display is undefined.
# Display all defined aliases
alias
alias -p
# Display specific defined aliases (assuming they exist in the current environment)
alias ls
alias ls grep
# Define or modify an alias
alias ls='ls --color=auto'
alias ls='ls --color=never' grep='grep --color=never'
Aliases set directly in the shell will be lost after the terminal is closed or the system is restarted. How can they be made permanent?
Open ~/.bashrc with an editor and add your alias settings, for example: alias rm='rm -i'. After saving, execute source ~/.bashrc to apply the changes. This will permanently save the command aliases.
Since this modifies the ~/.bashrc file in the current user's directory, it only affects the current user. To make it effective for all users, modify the /etc/bashrc file instead.
Note: The following may vary depending on your system:
On CentOS 7, this file is
/etc/bash.bashrc. Additionally, if you examine~/.bashrcon CentOS 7, you might find a block of code like this:if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fiThis code means that if
.bash_aliasesexists, it will be loaded. You can create this file in your home directory to store your alias settings separately.
# Clear all aliases for demonstration
unalias -a
# Not wrapped in single quotes
alias rm=rm -rf
# Execution results in an error: bash: alias: -rf: not found
# Checking with 'alias' returns: alias rm='rm'
# A more confusing example
# Clear all aliases for demonstration
unalias -a
# Still not wrapped in single quotes
alias ls=ls --color=never
# Execution appears to succeed without error
# Checking with 'alias' reveals:
# alias --color=never
# alias ls='ls'
# The 'alias' command treated these as two separate groups.
Q: What happens if I try to display multiple aliases and some are undefined?
A: Run it as usual; alias will continue to process the remaining arguments even if one is undefined.
Q: What if I define alias cd='ls' ls='cd'?
A: Running cd will still change the directory, and ls will still list files. Do not define aliases this way.
source command runs alias or unalias, it may affect the terminal environment's alias settings; conversely, terminal aliases can affect the script's results.sh or executed directly (with execution permissions) are not affected by terminal environment aliases.unalias command.-f in aliases for commands like mv, cp, or rm (e.g., alias rm='rm -f').help command.