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.

4
  • Is using a loop an option? Commented Aug 28, 2015 at 14:30
  • "t1","t2","t3" | % { "prefix" + $_ } Commented Aug 28, 2015 at 14:31
  • Maybe you can use 'splatting' stackoverflow.com/questions/448911/… Commented Aug 28, 2015 at 14:31
  • @arco444: no, loop is not a solution Commented Aug 28, 2015 at 14:53

2 Answers 2

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'})
Sign up to request clarification or add additional context in comments.

4 Comments

It is not a solution, but nice enough. Thanks.
@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.
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 I see. It's true, despite its name PowerShell is really lacking in the "shell" aspect. Perhaps you could try PSReadline module - it really tries to bring a *nix shell expirience to the PS (no list expansion, though).
1

A couple of other ways are:

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

and

't1', 't2', 't3' | % {'prefix{0}suffix' -f $_}

1 Comment

It is not a solution, but also nice. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.