Is there a shorter PowerShell equivalent to Bash brace expansion?
Asked by
PEBBLE BEACHCOMBER
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?
Answers
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.
Reply to this answer
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.
Reply to this answer
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`.
Reply to this answer
Add your answer
Answers are reviewed before publication.
quilt batting: 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.