Implementation method of shell alias setting alias for command

Time:2021-8-22

To alias a command, you can use it as the “nickname” of the command, but what’s the point of doing so?

For example, when I first came into contact with Linux, the editor I used was VI, but now the function of vim is obviously more powerful than that of VI, so the popular editor has become vim. However, the author is used to inputting VI command instead of vim command. Despite the small difference of “m”, he always feels uncomfortable when executing the command. At this time, the alias can work. As long as the alias of the VIM command is defined as VI, the VI command executed later actually runs the VIM command.

Command format:

[[email protected] ~]# alias
#Query command alias
[ [email protected] ~]# alias alias = 'original command'
#Set command alias

For example:

[[email protected] ~]# alias
#Query the aliases defined in the system
alias cp='cp -i'
alias l='ls -d .* --color=auto'
alias ll='ls -1 --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[ [email protected] ~]#Alias VI ='vim '# defines that the alias of the VIM command is VI
[[email protected] ~]# alias
#Re query alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -1 --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias vi='vim'
#Alias is already in effect
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

It should be noted that the command alias takes precedence over the command itself. Therefore, once the alias VI is set to the VIM command, the original VI command cannot be used. Therefore, unless you are sure that the original command is unnecessary, the alias cannot be the same as the system command. Another example:


[[email protected] ~]# alias sto='/usr/local/apache2/bin/apachectl stop'
[[email protected] ~]# alias sta='/usr/local/apache2/bin/ apachectl start'

When configuring and using Apache, we need to constantly restart Apache services. At this time, defining “sta” as the alias of Apache start command and “sto” as the alias of Apache stop command can effectively speed up the restart of Apache services.

Of course, the premise is that it has been determined that there are no “sta” and “sto” commands in the system, so these two aliases will not overwrite the system commands. So how to determine that there are no “sta” and “sto” commands in the system? Remember the whereis and which commands? In addition, using the tab command completion function can also determine whether these two commands are available.

Since we say that aliases have higher priority than commands, what is the specific order of command execution? The order of command execution is as follows:
The first sequence executes a command executed with an absolute path or a relative path.
The second sequence executes the alias.
The third sequence executes bash’s internal commands.
The fourth sequence executes the first command of directory search defined by the $path environment variable.

The alias is so simple, but if we use the command to define the alias, the alias will only take effect temporarily. Once you log off or restart the system, the alias will disappear immediately. To make this alias permanent, you can write the alias to the environment variable configuration file ~ /. Bashrc. The command is as follows:


[[email protected] ~】# vi /root/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias vi='vim'
alias sto='/usr/local/apache2/bin/apachectl stop' alias sta='/usr/local/apache2/bin/apachectl start'
#Source global definitions
if [ -f /etc/bashrc ];then
./etc/bashrc
fi

In this way, these aliases can take effect permanently. So what is the environment variable configuration file? The so-called environment variable configuration file, as the name suggests, is used to define our operating environment. Of course, the alias is also the operating environment. We will explain the function of this file in detail in the subsequent chapters.

Can the set alias be deleted? The alias can be easily deleted by executing the unalias command. The command is as follows:


[[email protected] ~]# unalias vi

Of course, if you are sure to delete the alias, you should also delete the related items in the environment variable configuration file.

This is the end of this article about the implementation method of shell alias setting alias for commands. For more information about setting alias for shell alias commands, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!