Powershell: variable Substitution/expansion

Created on 25 Mar 2020  路  3Comments  路  Source: PowerShell/PowerShell

all shell (bash/zsh/csh...etc) have variable Substitution/expansion even cmd.exe
why not powershell ?

for example: based on other shell:

# Remove the beginning or end of a string
PS> ${myvar#Pattern}    
    ${myvar##Pattern}

# Search and replace
PS> ${myvar/foo/baz}

# Make letters uppercase or lowercase
PS>   

# Remove pattern
PS> ${myvar%pattern}  ${myvar%%pattern}

# Substring
PS> ${myvar:offset}
    ${myvar:offset:length}
    ${myvar:position}

its breaking change but variable substitution is based in all shell and people use it a lot

Issue-Discussion Resolution-Answered WG-Language

Most helpful comment

These features are all available in other ways. Simply because they don't use an identical syntax does not mean the features are not available. 馃檪

<#
    Remove the beginning or end of a string
PS> ${myvar#Pattern}    
    ${myvar##Pattern}
#>
# to trim whitespace
$string.TrimStart() 
$string.TrimEnd()

# to trim both at once
$string.Trim() 

# to trim specific characters from start and end; you can do this with TrimStart and TrimEnd as well
$string.Trim($characters) 

```ps1
<#
Search and replace
PS> ${myvar/foo/baz}

>

Regex operators

$myvar -replace 'foo','baz'

```ps1
# Make letters uppercase or lowercase
$string.ToUpper()
$string.ToLower()

```ps1
<#
Remove pattern
PS> ${myvar%pattern} ${myvar%%pattern}

>

$string -replace 'pattern'

```ps1
<#
Substring
PS> ${myvar:offset}
    ${myvar:offset:length}
    ${myvar:position}
#>
$string.Substring($startIndex, $length)
$string.Substring($startIndex)

I would recommend you read up on what PowerShell can do, there's a huge amount of help documentation for PowerShell and for .NET which lays out how much it can do. I don't see a real need to reinvent the wheel here. 馃檪

Also, regarding syntax... we cannot have PowerShell use $variable:someOtherThing or ${variable:something} syntax for variables, since we already use : as a scope specifier in variables, and it can invoke PSProviders with that syntax. Additionally, ${var/thing/otherthing} is currently a perfectly legal variable name in PowerShell; it uses the ${} syntax to permit otherwise illegal characters to be used in variable names, and that doesn't seem like something that would be worthwhile to change in my opinion. ^^

All 3 comments

These features are all available in other ways. Simply because they don't use an identical syntax does not mean the features are not available. 馃檪

<#
    Remove the beginning or end of a string
PS> ${myvar#Pattern}    
    ${myvar##Pattern}
#>
# to trim whitespace
$string.TrimStart() 
$string.TrimEnd()

# to trim both at once
$string.Trim() 

# to trim specific characters from start and end; you can do this with TrimStart and TrimEnd as well
$string.Trim($characters) 

```ps1
<#
Search and replace
PS> ${myvar/foo/baz}

>

Regex operators

$myvar -replace 'foo','baz'

```ps1
# Make letters uppercase or lowercase
$string.ToUpper()
$string.ToLower()

```ps1
<#
Remove pattern
PS> ${myvar%pattern} ${myvar%%pattern}

>

$string -replace 'pattern'

```ps1
<#
Substring
PS> ${myvar:offset}
    ${myvar:offset:length}
    ${myvar:position}
#>
$string.Substring($startIndex, $length)
$string.Substring($startIndex)

I would recommend you read up on what PowerShell can do, there's a huge amount of help documentation for PowerShell and for .NET which lays out how much it can do. I don't see a real need to reinvent the wheel here. 馃檪

Also, regarding syntax... we cannot have PowerShell use $variable:someOtherThing or ${variable:something} syntax for variables, since we already use : as a scope specifier in variables, and it can invoke PSProviders with that syntax. Additionally, ${var/thing/otherthing} is currently a perfectly legal variable name in PowerShell; it uses the ${} syntax to permit otherwise illegal characters to be used in variable names, and that doesn't seem like something that would be worthwhile to change in my opinion. ^^

This has actually come up before: #9566

This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.

Was this page helpful?
0 / 5 - 0 ratings