Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the alias command in Linux

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use the alias command in Linux. Xiaobian thinks it is quite practical, so share it with you as a reference. I hope you can gain something after reading this article.

Executing a very long command in Linux system is quite troublesome, we can set alias under the.bashrc file, set short alias for the command, equivalent to shortening the command, convenient operation

alias Summary alias [-p] [name[=value]...] The main purpose is to simplify longer commands.

Define one or more aliases.

Modify the value of one or more defined aliases.

Displays one or more defined aliases.

Displays all defined aliases.

Option-p: Displays all defined aliases. Parameter name (optional): Specifies 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 #Show all defined aliases alias -p #Show defined aliases (assuming the following aliases exist in the current environment) alias ls alias ls grep #Define or modify alias values alias ls='ls --color=auto' alias ls='ls--color=never' grep='grep --color=never' Knowledge Point Command aliases set directly in the shell will expire after the terminal is shut down or the system is restarted. How can they be permanently valid?

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

Because you are modifying the ~/.bashrc file in the current user directory, this is only useful for the current user. To work with all users, modify the/etc/bashrc file.

Please note that the following may differ from your actual system:

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

if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi This code means 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 display is undefined.

When you define (modify) the value of an alias, serious problems arise because the string of values has spaces but you do not enclose them in single quotes:

#For ease of demonstration, delete all aliases unalias -a #not enclosed in single quotes alias rm=rm -rf #error bash: alias: -rf: not found #When using alias to check the alias of rm, alias rm='rm' #More confusing examples #For convenience of demonstration, Remove all aliases unalias -a #still not enclosed in single quotes alias ls=ls --color=never #No errors appear after executing the command #Use alias to view all aliases and you will find the following results: # alias --color=never # alias ls='ls' # alias processing treats them as two sets of Q&AQ: What if I want to display one or more aliases, but I don't know if any of them are undefined?

A: Normal execution is fine, alias does not end execution of the remaining parameters because there is an undefined alias.

Q: What happens if I define alias cd='ls' ls='cd'?

A: Running cd will still switch directories, and running ls will still list the contents of folders; don't define it that way.

Note When executing scripts:

The bash script executed with the source command may affect the alias setting of the terminal environment if the alias or unalias command is executed; the alias setting of the terminal environment may also change the running result;

Bash scripts invoked via sh or directly running scripts that the current user has permission to execute are not affected by the alias of the terminal environment.

To remove aliases, see the unalias command.

It is recommended that you do not set the dangerous-f option on aliases for commands such as mv cp rm, such as alias rm ='rm-f'.

Note that aliases conflict with other commands.

This command is a bash built-in command. For help information, please see the help command.

About "how to use alias command in Linux" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report