Do curly braces mean anything in PowerShell variable declaration?

穿精又带淫゛_ 提交于 2021-02-07 04:55:16

问题


I have come across variables (or parameters) being declared like this:

${var_name} = "Hello world!"

As far as I can tell, this is no different to the following:

$var_name = "Hello world!"

I am wondering if the {} braces in the first example do or mean anything. Do they change the behaviour of the variable?


回答1:


Curly braces in PowerShell variable names allow for arbitrary characters in the name of the variable. If there are no "pathological" characters in the variable name, then the braces are not needed and have no effect.

You'll find that "generated" code will sometimes use curly braces because they guarantee that the variable name is valid.




回答2:


the {} braces use for declare variables with spaces in the middle or inside of the variable, like this:

${var_name   hello   } = "Hello world!2"

$var_name = "Hello world!"

it's not the same, 'cause you can't save data in a variable with spaces, powershell understand the variable until a space, except it's inside the braces.

Have a good day. (:




回答3:


What might not be obvious on a first glance is that you can use any provider within ${}, for example:

${c:\tmp\foo.txt}="Hello World"       # C: uses file system provider

The effect depends on the provider, for the file system provider the example changes the content of the specified file.



来源:https://stackoverflow.com/questions/36773800/do-curly-braces-mean-anything-in-powershell-variable-declaration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!