bash shell的参数扩展$ {} [英] Bash Shell parameter expansion ${ }

查看:122
本文介绍了bash shell的参数扩展$ {}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太清楚怎么去理解这一点 -

 是$ var =''
$回声| $ {VAR} |
| |
$回声| $ {VAR // /} |
||

VS

  $集 - ''''''
$回声| $ {*} |
| |
$回声| $ {// * /} |
| |

然而,当我上面后添加此

  $ IFS =
$回声| $ {// * /} |
||

在第二组命令到底哪里出问题了?这是预期的结果?


解决方案

例1

 是$ var =''
$回声| $ {VAR} |
| |
$回声| $ {VAR // /} |
||

在这里,您有包含两个空格的一个简单的字符串。当两个管道字符之间展开,可以​​看到管道之间两个空格。当您使用模式替换从变量的扩展删除所有空格,你看到两个管之间的空字符串。

例2

  $集 - ''''''

首先,你每前三位置参数设置为空字符串。您可以通过比较 $ {1 - 富} {$ 4富} (其结果观察此如果显示设置参数,但是'富',如果它未设置)。

  $回声$ {1}富$回声$ {4}富

因此​​,我们可以看到, $ 1 设置,但空,而 $ 4'/ code>未设置。

  $回声| $ {*} |
| |

接下来,我们看到扩大特殊参数的结果 $ * 里面的引号,这是由该所设置的位置参数的一个字符串,由分隔在 IFS 参数的第一个字符。 IFS 默认情况下有一个空间作为第一个参数,所以我们看到的是一个由3个空串,每一个空间,这仅仅是一个单一的字符串分隔字符串第2位。

  $回声| $ {// * /} |
| |

当你申请模式替换为 $ * ,替代应用到所产生的参数,使用加入分开前,每个位置参数 IFS 。由于位置参数都已经空了,从他们身上去掉空格使他们保持不变。所以,你 $ * 本身得到同样的结果时,你只是作为扩大

例3

  $ IFS =
$回声| $ {// * /} |
||

下面的步骤是一样的例子2,具有重要的区别,现在 IFS 是空字符串,而不是它的'默认。再次,模式替换并没有真正做任何事情,因为没有空间可以从任何位置参数的删除。但现在,扩大 $ * 在由参数的位置,没有插入字符的字符串结果。相反 $ 1 $ 2 $ 3 ,你得到 $ 1 $ 2 $ 3 。由于三者都是自己空字符串,其结果是空字符串。

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

$ 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

$ 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).

$ echo ${1-foo}

$ echo ${4-foo}
foo

So we can see that $1 is set, but null, while $4 is unset.

$ 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 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.

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

When you apply pattern substitution to $*, the substitution is applied to each positional parameter separately before the resulting parameters are joined using 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

$ 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 ''. 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. Since all three are themselves empty strings, the result is the empty string.

这篇关于bash shell的参数扩展$ {}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆