accessing the $args array in powershell

后端 未结 1 1251
借酒劲吻你
借酒劲吻你 2020-12-29 21:22

I have a test powershell V2 script that looks like this:

    function test_args()
    {
      Write-Host \"here\'s arg 0: $args[0]\"
      Write-Host \"here\         


        
1条回答
  •  别那么骄傲
    2020-12-29 21:56

    Try this instead:

    function test_args()
    {
      Write-Host "here's arg 0: $($args[0])"
      Write-Host "here's arg 1: $($args[1])"
    }
    
    test_args foo bar
    

    Note that it is $args and not $arg. Also when you use a PowerShell variable in a string, PowerShell only substitutes the variable's value. You can't directly use an expression like $args[0]. However, you can put the expression within a $() sub-expression group inside a double-quoted string to get PowerShell to evaluate the expression and then convert the result to a string.

    0 讨论(0)
提交回复
热议问题