bash 脚本中的反引号和 $() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8941381/
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 01:22:37  来源:igfitidea点击:

What is the difference between backticks and $() in bash script?

bashbackticks

提问by Misha Moroshko

I see in bash scripts two different forms which seems to do the same:

我在 bash 脚本中看到两种不同的形式,它们似乎做同样的事情:

`some command`

and

$(some command)

What is the difference between the two, and when should I use each one of them?

两者有什么区别,我应该什么时候使用它们?

采纳答案by thiton

There is no semantic difference. The backtick syntax is the older and less powerful version. See man bash, Section "Command Substitution".

没有语义差异。反引号语法是较旧且功能较弱的版本。请参阅man bash,“命令替换”部分。

If your shell supports the $()syntax, prefer it because it can be nested.

如果您的 shell 支持该$()语法,请首选它,因为它可以嵌套。