文章目录
- Mastering the Linux `alias` Command(精通Linux的`alias`命令)
- 1. Understanding the `alias` Command(理解`alias`命令)
- 示例
- Ubuntu20.04 arm操作系统
- OpenEuler20.03 arm操作系统
- 2. Basic Usage of `alias`(`alias`的基本用法)
- 3. Advanced Applications of `alias`(`alias`的高级应用)
- 4. Making Aliases Permanent(使别名永久有效)
- 5. Tips and Tricks(技巧和提示)
- 1. Use the `unalias` command to remove an alias.(使用`unalias`命令删除一个别)名。
- 2. Enclose commands containing spaces in quotes when defining an alias.(在定义包含空格的命令的别名时,将命令用引号括起来。)
- 3. Remember that aliases are specific to the shell they are defined in.
- 6. Conclusion(结论)
Mastering the Linux alias
Command(精通Linux的alias
命令)
The Linux alias
command is a built-in shell command that allows users to create shortcuts or synonyms(同义词) for other commands. It simplifies complex command-line instructions, saving time and minimizing potential mistakes.
Linux的alias
命令是一个内置的shell命令,允许用户为其他命令创建快捷方式或同义词。它简化了复杂的命令行指令,节省了时间并尽可能减少了错误。
1. Understanding the alias
Command(理解alias
命令)
The alias
command in Linux is an inbuilt(内置的) command that creates shortcuts for other commands which can include options and arguments. This can significantly(显著地) streamline(使效率更高) your workflow(工作流程), particularly when you frequently use long or complex commands.
Linux中的alias
命令是一个内置命令,可以创建其他命令的快捷方式,其中可以包括选项和参数。这可以显著简化你的工作流程,特别是当你频繁使用长的或复杂的命令时。
To view all currently defined aliases in the terminal, simply type alias
and press enter:
要查看终端中当前定义的所有别名,只需输入alias
然后按回车键:
alias
示例
Ubuntu20.04 arm操作系统
OpenEuler20.03 arm操作系统
2. Basic Usage of alias
(alias
的基本用法)
The general syntax of the alias
command is as follows:
alias
命令的一般语法如下:
alias alias_name='command'
The alias_name
is the name of the new alias, and command
is the command that the alias refers to.
alias_name
是新别名的名称,command
是别名所指向的命令。
For instance(例如), to create a shortcut for the ls -lh
command (which lists files in ‘long format’ with human-readable file sizes), one could define an alias named ll
:
例如,为了创建ls -lh
命令的快捷方式(它以"长格式"列出文件,文件大小可读),可以定义一个名为ll
的别名:
alias ll='ls -lh'
Now, typing ll
in the terminal would execute ls -lh
.
现在,在终端中输入ll
将执行ls -lh
。
3. Advanced Applications of alias
(alias
的高级应用)
Aliases can be more complex and powerful. They can incorporate(包含) multiple commands separated by semicolons(分号) or contain control structures such as loops or conditional statements.
别名可以更复杂和强大。他们可以包含由分号分隔的多个命令,或包含循环或条件语句等控制结构。
For example, an alias could be created to update the system and then clear the terminal:
例如,可以创建一个别名来更新系统然后清除终端:
alias update_and_clear='sudo apt-get update; clear'
4. Making Aliases Permanent(使别名永久有效)
Aliases defined in a terminal(终端) session(会话) are temporary - they cease(停止) to exist when the session is closed. To make an alias permanent, it needs to be defined in the shell configuration file.
在终端会话中定义的别名是临时的——当会话关闭时,它们就不存在了。要使别名永久有效,需要在shell配置文件中定义它。
注:"Terminal"或者"终端"在操作系统中,通常指的是一个用于接收用户输入命令和显示系统输出信息的接口。它通常是文本模式下的操作环境,没有图形化界面。在Windows系统中,最常见的类似工具是命令提示符(Command
Prompt)或PowerShell;而在Unix/Linux系统中,我们通常指的是shell环境。使用终端可以让用户直接通过命令行来控制操作系统,执行各种任务,如管理文件系统、安装和删除软件包、启动和停止服务等。对于熟悉命令行的用户来说,这种方式往往比图形化界面更高效。
注:"Cease"和"stop"区别:
"Cease"和"stop"都有停止的意思,但它们的用法和语境有所不同。"Stop"是一个更常见、更口语化的词汇,可用于各种情况,例如 “stop talking”(停止说话),“stop
running”(停止跑步)等。这个词可以在非正式或日常对话中使用。"Cease"则更正式,常出现在书面语言或法律文件中,比如 “cease and desist”(立即停止)。通常用于表达某事物完全、永久性地结束。例如,“The company ceased operations”(公司停止了运营)。这个词通常不会在日常对话中使用。
For bash
, this would be the ~/.bashrc
file. For zsh
, this would be the ~/.zshrc
file. An example of defining an alias permanently for bash
would be:
对于bash
,这将是~/.bashrc
文件。对于zsh
,这将是~/.zshrc
文件。以下是为bash
永久定义别名的例子:
echo "alias ll='ls -lh'" >> ~/.bashrc
source ~/.bashrc
5. Tips and Tricks(技巧和提示)
Here are a few additional tips and tricks that might come in handy:
以下是一些可能派得上用场的额外技巧和提示:
1. Use the unalias
command to remove an alias.(使用unalias
命令删除一个别)名。
Explanation: The unalias
command is used in Unix/Linux based systems to remove or delete the alias that was previously created.
解释:unalias
命令用于Unix/Linux基础系统中,用来移除或删除之前创建的别名。
Example: Suppose we have created an alias like this: alias ls='ls -al'
. If we want to remove this alias, we can use unalias ls
.
示例:假设我们创建了这样一个别名:alias ls='ls -al'
。如果我们想要移除这个别名,我们可以使用unalias ls
。
2. Enclose commands containing spaces in quotes when defining an alias.(在定义包含空格的命令的别名时,将命令用引号括起来。)
Explanation: When you are creating an alias and the command contains space, then you need to enclose(括起来) the whole command in single or double quotes(引号).
解释:当你在创建一个别名,并且命令包含空格时,你需要用单引号或双引号把整个命令括起来。
Example: To create an alias for the command ls -al
, which has a space, you can write it as alias ls='ls -al'
.
示例:为了创建一个包含空格的命令ls -al
的别名,你可以写成alias ls='ls -al'
。
3. Remember that aliases are specific to the shell they are defined in.
记住,别名是特定于它们被定义的shell。
Explanation: Aliases are specific to the shell they are created in, and they will not be available in a new shell or a different terminal session unless they are defined in the shell’s configuration file.
解释:别名是特定于创建它们的shell,除非在shell的配置文件中定义,否则在新的shell或不同的终端会话中不可用。
Example: If you define alias ls='ls -al'
in one terminal session, then open a new terminal window, the alias will not be recognized in the new window.
示例:如果你在一个终端会话中定义了alias ls='ls -al'
,然后打开一个新的终端窗口,那么在新窗口中将无法识别这个别名。
6. Conclusion(结论)
The Linux alias
command can greatly simplify the command-line interface and increase productivity (生产率)by reducing the time and effort required to type long or complex commands. With a good understanding of how to create, use, and manage aliases, you’re well on your way to becoming a more efficient Linux user.
Linux的alias
命令可以大大简化命令行界面,并通过减少输入长的或复杂的命令所需的时间和努力来提高生产力。通过对如何创建、使用和管理别名的良好理解,你将很快成为一个更有效率的Linux用户。