Welcome › Forums › General PowerShell Q&A › why does this not evaluate to 6
- This topic has 21 replies, 6 voices, and was last updated 11 months, 1 week ago by
Participant.
-
AuthorPosts
-
-
February 23, 2020 at 2:57 pm #205434
$x = 5<br />$y = $x++<br />write-host $y
why is $y evaluating to 5 and not 6?
-
February 23, 2020 at 3:31 pm #205446
$x++ doesn’t return anything, instead it increments variable x’s value by 1. If you wan’t it to return, just wrap it in parenthesis.
PowerShell1$y = ($x++)-
This reply was modified 11 months, 1 week ago by
kvprasoon. Reason: post correction
-
This reply was modified 11 months, 1 week ago by
-
February 23, 2020 at 3:40 pm #205452
-
February 23, 2020 at 3:55 pm #205458
It should work, it is called Variable Squeezing in PowerShell.
And as kvprasoon mentioned, $x++ will not return any value, it is used for self increment, so it can’t be assigned to $y.
Either you can use $y = $x + 1 or $y = ($x++)
Thank you.
-
This reply was modified 11 months, 1 week ago by
Kiran. Reason: Spelling correction
-
This reply was modified 11 months, 1 week ago by
-
February 23, 2020 at 4:14 pm #205467
Thank you for your help
I suspect I am doing something wrong or missing something obvious. I opened up a new shell and type the following:
PowerShell123456[crayon-601149111b31b206245299 inline="true" ]PS C:\Users\superuser> $x = 5PS C:\Users\superuser> $x5PS C:\Users\superuser> $y = ($x++)PS C:\Users\superuser> $y5-
This reply was modified 11 months, 1 week ago by
Shane.
-
This reply was modified 11 months, 1 week ago by
-
February 23, 2020 at 4:27 pm #205476
Okay, what is your windows? is it Server edition or Client Edition?
I think variable squeezing won’t work in Server side PowerShell.
-
February 23, 2020 at 4:39 pm #205479
@Kiran does it really work for you on your systems? I checked it on my client W10 1909, Windows Powershell 5.1 or Powershell 7.0.0-rc.3. The result is the same on both versions.
PowerShell123456PS C:\> $x = 5PS C:\> $x5PS C:\> $y = ($x++)PS C:\> $y5 -
February 23, 2020 at 4:46 pm #205482
[Edit]
Yes, I checked it on my Windows 10 1909 (itself) and it is working on Windows PowerShell 5.1,
PowerShell Core 6.2.3 and PowerShell 7 rc3 as welland not on 6.2.3 & 7.0.0-rc3 (sorry for the little confusion)But not working on Window Server 2019 with Windows PowerShell 5.1
-
This reply was modified 11 months, 1 week ago by
Kiran. Reason: Correction
-
This reply was modified 11 months, 1 week ago by
-
February 23, 2020 at 4:46 pm #205485
-
February 23, 2020 at 4:47 pm #205488
Yes, I checked it on my Windows 10 1909 (itself) and it is working on Windows PowerShell 5.1, PowerShell Core 6.2.3 and PowerShell 7 rc3 as well
But not working on Window Server 2019 with Windows PowerShell 5.1
interesting. Thanks
-
February 23, 2020 at 5:05 pm #205506
I have double checked and noticed the below…
Win PS 5.1 PS Core 6.2.3 PS 7.0.0-rc3 Windows Native Console Windows Terminal (Preview) Native Console Windows Terminal (Preview) Native Console Windows Terminal (Preview) Windows 10 1909 YES NO NO NO NO NO Windows Server 2019 NO It looks very weird behavior.
-
This reply was modified 11 months, 1 week ago by
Kiran.
-
This reply was modified 11 months, 1 week ago by
-
February 23, 2020 at 7:19 pm #205530
++ after the variable increments after sending the value, ++ before the variable increments before sending the value.
PowerShell12345$x = 5$y = ++$x$y6There’s a similar thing in bash (or C):
PowerShell12345x=5y=$((++x))echo $y6 -
February 23, 2020 at 9:50 pm #205542
-
February 23, 2020 at 9:56 pm #205548
I don’t know what you mean. It works the way I said, in ps 5 and 7, with or without parentheses.
-
February 23, 2020 at 10:11 pm #205551
OK, but obviously it does not work as expected for all systems. Kiran confirmed it and it does not work on my system.
-
February 23, 2020 at 10:14 pm #205554
Kirin is mistaken.
-
February 23, 2020 at 10:16 pm #205557
-
February 24, 2020 at 2:19 am #205575
-
February 24, 2020 at 2:22 am #205578
-
February 24, 2020 at 12:54 pm #205665
For what it is worth, I was going through basic code shown in the book, “Windows’ PowerShell Programming for the absolute beginner” third edition, when I found this abnormality, They were using powershell 4 in the book.
-
February 24, 2020 at 1:56 pm #205686
Maybe you have something weird in your $profile?
-
February 24, 2020 at 6:53 pm #205779
Here is what I get. JS is correct.
PS C:\> $x = 5
PS C:\> $x
5
PS C:\> $y = ($x++)
PS C:\> $y
5
PS C:\> $y = ($x++)
PS C:\> $y
6
PS C:\> $y = ($x++)
PS C:\> $y
7
PS C:\>
-
-
AuthorPosts
- The topic ‘why does this not evaluate to 6’ is closed to new replies.