Bash 命令:变量和别名有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7342735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:43:31  来源:igfitidea点击:

Bash Command: What's the difference between a variable and an alias?

linuxbashubuntuterminal

提问by Rei

I'm new to linux and starting from the basics.

我是 linux 新手,从基础开始。

-- I thought alias is used to make a shortcut to a command. But I tried the following using a variable (in Ubuntu) and still works!

-- 我认为 alias 用于创建命令的快捷方式。但是我使用变量(在 Ubuntu 中)尝试了以下操作并且仍然有效!

$ foo="mkdir Directory"
$ $foo #this will create a directory named Directory

using alias:

使用别名:

$ alias bar="mkdir Directory"
$ bar #creates a Directory named directory

Is that how it is supposed to work? Many thanks for the answers :)

这是应该的工作方式吗?非常感谢您的回答:)

回答by Adam Rosenfield

Variables are much more versatile than aliases. Variables can be used anywhere in a command line (e.g. as parts of program arguments), whereas aliases can onlybe used as the names of programs to run, i.e. as the first word in a command line. For example:

变量比别名更通用。变量可以在命令行的任何地方使用(例如作为程序参数的一部分),而别名只能用作要运行的程序的名称,即作为命令行中的第一个单词。例如:

foo="mkdir Directory"
echo $foo  # Prints "mkdir Directory"

alias bar="mkdir Directory"
echo bar  # Nothing gets expanded -- just "bar" is printed

Variables can also be exported into the environment of child processes. If you use the exportbuiltinto export variables, then programs can use getenv(3)function to get the variables' values.

变量也可以导出到子进程的环境中。如果您使用export内置getenv(3)函数导出变量,则程序可以使用函数来获取变量的值。

See the Bash manualfor a full description of all of the different types of expansions it can perform and how it performs them. See also the section on aliases.

有关它可以执行的所有不同类型的扩展及其执行方式的完整说明,请参阅Bash 手册。另请参阅别名部分。

回答by Dan King

"Variables are much more versatile than aliases" - not completely true!

“变量比别名更通用” - 不完全正确!

For executing commands, variables have some limitations which are not shared by aliases.

对于执行命令,变量有一些别名不共享的限制。

Try using a variable to define a command that echoes a single asterisk to the command line:

尝试使用变量来定义一个命令,该命令将单个星号回显到命令行:

$ myCommand="echo *"
$ $myCommand
file1.x file2.y file3.z

The * is expanded - which isn't what you wanted - so then try this:

* 被扩展 - 这不是你想要的 - 所以试试这个:

$ myCommand="echo \"*\""
$ $myCommand
"*"

But now you have extra quotes appearing - you didn't want that either!

但是现在您出现了额外的引号-您也不想要!

But this works OK:

但这可以正常工作:

$ echo "*"
*

And so does this:

这也是如此:

$ alias myCommand="echo \"*\""
$ myCommand
*

This is because quotes inside a variable are treated as literal, rather than syntactical when a variable is expanded - so in this case they become part of the parameter that is passed to the echo command - whereas with an alias the command is executed "as is" and the quotes are treated as syntactical and are parsed out before echo is called, just as they are when you type the same command in directly from the command line.

这是因为变量内的引号在变量扩展时被视为文字,而不是语法 - 因此在这种情况下,它们成为传递给 echo 命令的参数的一部分 - 而使用别名命令“按原样”执行" 并且引号被视为语法并在调用 echo 之前被解析,就像直接从命令行键入相同的命令一样。

回答by Foo Bah

In the variable form, it is expanded and evaluated.

在变量形式中,它被扩展和评估。

Hence

因此

$ $foo

is expanded to

扩展为

$ mkdir Directory

which can be evaluated. It's the same type of expansion as if foo were an argument:

可以评估的。它的扩展类型与 foo 是参数的类型相同:

$ echo $USER

For aliases, you reference them directly by using the name (no $) and it is only expanded if it is the first word.

对于别名,您可以通过使用名称(无 $)直接引用它们,并且仅在它是第一个单词时才进行扩展。

回答by Ernest Friedman-Hill

Notice that when you type the alias as if it were a command, you don't type the $; when you type a variable, you do have to include it. So aliases act more naturally as an alternate name for a command.

请注意,当您像输入命令一样键入别名时,不要键入$; 当你输入一个变量时,你必须包含它。因此,别名更自然地充当命令的替代名称。

回答by Ignacio Vazquez-Abrams

Variables replace their content wherever they are used.

变量在任何使用它们的地方都会替换它们的内容。

$ foo="mkdir Directory"
$ $foo
$ echo "$foo"

Aliases act as actual commands, or rather the prefix of one.

别名充当实际命令,或者更确切地说是一个命令的前缀。

$ alias bar="mkdir Directory"
$ bar directory2  #creates directories named "directory" and "directory2"
$ echo bar