0

In bash I wrote
echo prefix{t1,t2,t3}suffix
and got
prefixt1suffix prefixt2suffix prefixt3suffix

Is something like this present in PowerShell? List expantion I mean.

OlegG
  • 975
  • 3
  • 10
  • 30

2 Answers2

1

There is no automatic expansion, but you can do it easily with ForEach-Object cmdlet or array's ForEach method in PS 4 and higher:

't1', 't2', 't3' | ForEach-Object {'prefix' + $_ + 'suffix'}

@('t1', 't2', 't3').ForEach({'prefix' + $_ + 'suffix'})
beatcracker
  • 6,714
  • 1
  • 18
  • 41
  • It is not a solution, but nice enough. Thanks. – OlegG Aug 31 '15 at 05:40
  • @OlegG I'm curious, do you have any specific reasons, that require the list expansion to be absolutely the same as in bash? Because I believe that internally bash loops through the collection too, there is no other way to do it. PowerShell just doesn't have this particular shortcut, that's all. – beatcracker Aug 31 '15 at 13:22
  • Just compare Bash command line `vim -d /some/directory/file{1,2}.ps1` with that I have write in PowerShell and you'll see my reason. – OlegG Sep 01 '15 at 19:59
  • @OlegG I see. It's true, despite its name PowerShell is really lacking in the "shell" aspect. Perhaps you could try [PSReadline module](https://github.com/lzybkr/PSReadLine) - it really tries to bring a *nix shell expirience to the PS (no list expansion, though). – beatcracker Sep 02 '15 at 02:07
1

A couple of other ways are:

't1', 't2', 't3' | % {"prefix$($_)suffix"}

and

't1', 't2', 't3' | % {'prefix{0}suffix' -f $_}
Dave Sexton
  • 10,768
  • 3
  • 42
  • 56