How to set command alias in linux system

It is necessary to enter the /var/www/site/mycitsm/ directory frequently, and each time you have to repeatedly enter this long string of paths to enter the directory, which is troublesome and time-consuming. Is there a good way to "cd /var/" www/site/mycitsm" to take an alias, and every time you only need to enter the alias to enter the directory?

Fortunately, the Linux system provides a useful tool called alias, which allows us to set up an alias for some frequently used but too verbose commands, so that we can achieve the same result by simply entering a short alias in the future. effect.

Usage: alias [-p] [name[=value] ... ] Note that no spaces can be included between '=' and the string

Display the currently set alias:
shell>alias -p
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
alias vi='vim'
alias which='alias | /usr/bin/which -- tty-only --read-alias --show-dot --show-tilde'

or enter directly:
shell>alias -p
alias l.='ls -d .* --color=tty'
alias ll='ls - l --color=tty'
alias ls='ls --color=tty'

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'If

you only want to display the meaning of an alias, you can enter the alias name, for example:
shell>alias ll
alias ll='ls -l --color=tty'If

you want to set an alias for a command, you can enter alias new command='original command options/parameters', for example:
shell>alias site='cd /var /www/site/mycitsm/'

If you want to cancel an alias, you can enter the unalias name, such as
shell>unalias site

. However, there is a problem with the above setting method, that is, the set command alias is only valid for the current session. Once the connection is disconnected and restarted Even the aliases set before are no longer valid.

Aliases can be made persistent by writing the command to set the alias to the startup file. Most Linux distributions use one of the following three startup files:
$HOME/.bash_profile
$HOME/.bash_login
$HOME/.profile
The command to set the alias can be written into the startup file, so that every time you connect to the system aliases will take effect. If you want the command to take effect immediately after the command is written to the startup file, remember to execute the source command, for example:
source $HOME/.bash_profile

Setting the command alias in the above way solves the problem that the command alias is only valid for the session, but the command alias written in the startup file under the specific home directory of each user is valid only for the user. It has no effect on other users, which is usually what you would expect to see under normal circumstances. But if it is true that the set alias is valid for any user, the command to set the alias can be written into the global startup file, such as /etc/profile.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326409312&siteId=291194637