简体   繁体   English

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

[英]What is the difference between two $ in Linux script

What is the difference between variable $processid ($) and Date=$(date +'%m-%d-%y')($) .变量$processid ($)Date=$(date +'%m-%d-%y')($)什么区别。 Here we are using two kinds of $ symbols, please let me know what is the difference of those two $ .这里我们使用了两种$符号,请告诉我这两种$的区别是什么。

From bash manual :bash 手册

3.5.3 Shell Parameter Expansion 3.5.3 Shell 参数扩展
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}.参数扩展的基本形式是 ${parameter}。 The value of parameter is substituted.参数的值被替换。

Note, that braces { and } are optional...请注意,大括号{}是可选的...

... ...
3.5.4 Command Substitution 3.5.4 命令替换
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. Bash 通过在子 shell 环境中执行命令并用命令的标准输出替换命令替换来执行扩展,并删除任何尾随换行符。

So if:因此,如果:

a=1

then $a and ${a} expand to 1 .然后$a${a}扩展为1

The following:下列:

a=$(echo 2)

executes the command echo with a single argument 2 in a subshell and grabs it's standard output ( echo 2 will write 2 with a newline on standard output).在子 shell 中使用单个参数2执行命令echo并获取它的标准输出( echo 2将在标准输出上用换行符写入2 )。 So it expands to:所以它扩展为:

a=2$'\n'

But trailing newlines are removed, so:但是尾部的换行符被删除,所以:

a=2

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

Answer: 1.Date=$(date +'%m-%d-%y') -> this $ will execute the command(date +'%m-%d-%y')答案:1.Date=$(date +'%m-%d-%y') -> 这个$会执行命令(date +'%m-%d-%y')

2.$processid -> this $ will expand the Processid Variable. 2.$processid -> 这个 $ 将扩展 Processid 变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM