Powershell equivalent of Bash Brace Expansion for generating lists/arrays

前端 未结 3 521
走了就别回头了
走了就别回头了 2020-12-30 05:11

When writing a Bash script you can use brace expansion to quickly generate lists:

\"Bash

3条回答
  •  醉梦人生
    2020-12-30 05:44

    I'm hoping to be proven wrong here, but I don't believe there is a way to do it exactly like with bash, or with as few keystrokes.

    You can iterate over the list by piping it through a foreach-object to achieve the same result though.

    1..5 | foreach-object { "test" + $_ }

    Or using the shorthand:

    1..5 | %{"test$_"}

    In both cases (% is an alias for foreach-object), the output is:

    test1
    test2
    test3
    test4
    test5
    

    Note: if you're building this into a script for publishing/distribution/reuse, use the more verbose foreach-object, not the shorthand % - for readability.

提交回复
热议问题