Stephane Rolland
Stephane Rolland

Reputation: 39916

Why does this function returns '' instead of a concatenated string

I have numerous fields to quote or double quote depending on the cases, so I have made a functions to do that.

Namely

function Add-SingleQuotes 
{  
    param([string] $input)

    $str_return = "'" + $input + "'" 
    return $str_return
}

However the result of this function is '', whatever the input I give. Why is it so ?

On the contrary, if I enter manually "'" + "4" + "'" the result is indeed '4'.

Upvotes: 2

Views: 56

Answers (2)

iRon
iRon

Reputation: 23713

$Input is indeed reserved and holds the object(s) in the pipeline. This is how you might use the $Input variable in your script:

function Add-SingleQuotes {  
    return "'" + $input + "'"
}

4 | Add-SingleQuotes

Which results into '4'

Upvotes: 1

Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58471

$input seems to be a reserved word but I can't find a reference to it that makes sense to me.

  1. $input gotchas perhaps

Changing it to $inp works for me.

Upvotes: 3

Related Questions