简体   繁体   English

Bash Shell参数扩展$ {}

[英]Bash Shell parameter expansion ${ }

I am not quite sure how to understand this- 我不太确定如何理解这一点-

$ var='  '
$ echo "|${var}|"
|  |
$ echo "|${var// /}|"
||

Vs. 比。

$ set -- '' '' ''
$ echo "|${*}|"
|  |
$ echo "|${*// /}|"
|  |

However, when I add this after the above 但是,当我在上面之后添加

$ IFS=
$echo "|${*// /}|"
||

What is going wrong in the second set of commands? 第二组命令出了什么问题? Is this the expected outcome? 这是预期的结果吗?

Example 1 例子1

$ var='  '
$ echo "|${var}|"
|  |
$ echo "|${var// /}|"
||

Here you have a simple string consisting of two spaces. 在这里,您有一个包含两个空格的简单字符串。 When you expand it between two pipe characters, you see two spaces between the pipes. 在两个管道字符之间展开它时,会在管道之间看到两个空格。 When you use pattern substitution to remove all the spaces from the expansion of the variable, you see the empty string between two pipes. 当您使用模式替换从变量扩展中删除所有空格时,您会看到两个管道之间的空字符串。

Example 2 例子2

$ set -- '' '' ''

First, you've set each of the first three positional parameters to the empty string. 首先,将前三个位置参数中的每一个都设置为空字符串。 You can observe this by comparing the results of the ${1-foo} with {$4-foo} (which displays the parameter if set, but 'foo' if it is unset). 您可以通过将${1-foo}{$4-foo}的结果进行比较来观察这一点(如果设置了则显示参数,如果未设置则显示'foo')。

$ echo ${1-foo}

$ echo ${4-foo}
foo

So we can see that $1 is set, but null, while $4 is unset. 因此,我们可以看到设置了$1 ,但设置为空,而未设置$4

$ echo "|${*}|"
|  |

Next, we see the result of expanding the special parameter $* inside quotation marks, which is a single string consisting of the positional parameters that are set, separated by the first character of the IFS parameter. 接下来,我们看到在引号内扩展特殊参数$*的结果,该引号是由设置的位置参数组成的单个字符串,由IFS参数的第一个字符分隔。 IFS by default has a space as its first parameter, so what we see is a string that consists of 3 empty strings, each separated by a space, which is just a single string of 2 spaces. 默认情况下, IFS的第一个参数有一个空格,因此我们看到的是一个由3个空字符串组成的字符串,每个字符串由一个空格分隔,这只是2个空格的单个字符串。

$ echo "|${*// /}|"
|  |

When you apply pattern substitution to $* , the substitution is applied to each positional parameter separately before the resulting parameters are joined using IFS . 当您将模式替换应用于$* ,替换将分别应用到每个位置参数,然后使用IFS结果参数。 Since the positional parameters are already empty, removing spaces from them leaves them unchanged. 由于位置参数已经为空,因此从中删除空格将使它们保持不变。 So you get the same result as when you just expanded $* by itself. 因此,您得到的结果与仅扩展$*时的结果相同。

Example 3 例子3

$ IFS=
$ echo "|${*// /}|"
||

The procedure here is the same as in example 2, with the important difference that now IFS is the null string, rather than its default of ''. 这里的过程与示例2中的过程相同,但重要的区别在于,现在IFS是空字符串,而不是其默认值''。 Once again, the pattern substitution doesn't really do anything, because there are no spaces to remove from any of the positional parameters. 再一次,模式替换实际上并没有做任何事情,因为没有位置要从任何位置参数中删除。 But now, expanding $* results in a string consisting of the positional parameters with no intervening characters. 但是现在,扩展$*导致一个字符串,该字符串由位置参数组成,中间没有字符。 Instead of $1 $2 $3 , you get $1$2$3 . 而不是$1 $2 $3 ,而是得到$1$2$3 Since all three are themselves empty strings, the result is the empty string. 由于所有三个本身都是空字符串,因此结果是空字符串。

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

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