gpt4 book ai didi

linux - 脚本中 $(command) 和 `command` 之间的区别

转载 作者:太空宇宙 更新时间:2023-11-04 04:48:41 29 4
gpt4 key购买 nike

执行这样的命令有什么区别:

var=$(ls -alh /dir)

这样做:

var=`ls -alh /dir`

一种方法是否能够比另一种方法在更多的解释器中使用?

最佳答案

它们都是命令替换

第 3.5.4 节 http://www.gnu.org/software/bash/manual/bashref.html

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by ‘$’, ‘`’, or ‘\’. The first backquote not preceded by a backslash terminates the command substitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.

Command substitutions may be nested. To nest when using the backquoted form, escape the inner backquotes with backslashes.

If the substitution appears within double quotes, word splitting and filename expansion are not performed on the results.

关于linux - 脚本中 $(command) 和 `command` 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16735280/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com