Is there a shorter PowerShell equivalent to Bash brace expansion?

0
0
Asked By MellowCedar42 On

I'm looking for a concise PowerShell equivalent to the shell command `touch intl_{en,ar,fr,sw}.arb`, which creates four empty files: `intl_en.arb`, `intl_ar.arb`, `intl_fr.arb`, and `intl_sw.arb`. Does PowerShell support brace expansion, or is there a short idiomatic way to generate these filenames and create the files?

3 Answers

Answered By ClearPebble7 On

PowerShell doesn’t have native brace expansion like Bash. The straightforward equivalent is to pipe the language codes into `ForEach-Object` and create each file: `'en','ar','fr','sw' | ForEach-Object { New-Item "intl_$_.arb" }`. `ForEach-Object` is commonly shortened to `%`, and `New-Item` can also be abbreviated as `ni`.

QuietHarbor19 -

You can also use PowerShell’s delay-binding script blocks to avoid an explicit `ForEach-Object`: `'en','ar','fr','sw' | New-Item -Path { "intl_$_.arb" } -Value ''`. The empty `-Value` prevents the piped language code from being written into the files.

Answered By NorthwindEcho26 On

For a very compact version, you could write `-split 'en ar fr sw' | % { ni "intl_$_.arb" }`. It works, but the more explicit `ForEach-Object` and `New-Item` names are usually easier to read and maintain.

Answered By SwiftLantern58 On

The key difference is that Bash performs brace expansion before running the command, while PowerShell generally generates the values programmatically. For a small fixed list, an array plus a pipeline is the clearest approach; there isn’t a built-in `{en,ar,fr,sw}` shorthand.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.