gpt4 book ai didi

linux - Linux脚本中两个$有什么区别

转载 作者:太空宇宙 更新时间:2023-11-04 05:47:31 26 4
gpt4 key购买 nike

变量$processid ($)Date=$(date +'%m-%d-%y')($)有什么区别。这里我们使用了两种$符号,请告诉我这两种$有什么区别。

最佳答案

来自bash manual :

3.5.3 Shell Parameter Expansion
The ‘$’ character introduces parameter expansion, command substitution, or arithmetic expansion. The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name.
...
The basic form of parameter expansion is ${parameter}. The value of parameter is substituted.

请注意,大括号 {} 是可选的...

...
3.5.4 Command Substitution
Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed as follows:
$(command)
....
Bash performs the expansion by executing command in a subshell environment and replacing the command substitution with the standard output of the command, with any trailing newlines deleted.

所以如果:

a=1

然后 $a${a} 扩展为 1

以下内容:

a=$(echo 2)

在子 shell 中使用单个参数 2 执行命令 echo 并获取其标准输出(echo 2 将在标准输出上写入带有换行符的 2)。所以它扩展到:

a=2$'\n'

但是尾随换行符被删除,因此:

a=2

所以 ${there:1:2} ${are:-} ${many//.*/ways} ${to##use} ${the,,} ${dollar^^$} $(( ${!sign} + ${#in} )) ${!Bash*}

关于linux - Linux脚本中两个$有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52816691/

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