powershell - unary increment operator within a string doesn't work -
suppose set variable, $i = 0, then
"-$i-" => "-0-" "-$($i)-" => "-0-"
the above expect, fails:
"-$(++$i)-" => "--" # expected "-1-"
however, expressions work. example:
"-$(pwd)-" => "-c:\temp-"
so what's going on here?
according documentation, expected behavior
the increment operator (++) increases value of variable 1. when use increment operator in simple statement, no value returned. view result, display value of variable, follows:
c:\ps> $a = 7 c:\ps> ++$a c:\ps> $a 8
to force value returned, enclose variable , operator in parentheses, follows:
c:\ps> $a = 7 c:\ps> (++$a) 8
the shortest way around following statement:
"-$((++$i))-" #=> -4-
since set of parentheses tell return value, opposed execute statement.
Comments
Post a Comment