W3cubDocs

/Nushell

str expand for strings

Generates all possible combinations defined in brace expansion syntax.

Signature

> str expand {flags}

Flags

  • --path, -: Replaces all backslashes with double backslashes, useful for Path.

Input/output types:

input output
list<string> list<list<string>>
string list<string>

Examples

Define a range inside braces to produce a list of string.

>"{3..5}"|str expand
╭───┬───╮
03
14
25
╰───┴───╯

Ignore the next character after the backslash ('')

>'A{B\,,C}'|str expand
╭───┬─────╮
0AB, 
1AC
╰───┴─────╯

Commas that are not inside any braces need to be skipped.

>'Welcome\, {home,mon ami}!'|str expand
╭───┬───────────────────╮
0Welcome, home!
1Welcome, monami!
╰───┴───────────────────╯

Use double backslashes to add a backslash.

>'A{B\\,C}'|str expand
╭───┬─────╮
0AB\
1AC
╰───┴─────╯

Export comma separated values inside braces ({}) to a string list.

>"{apple,banana,cherry}"|str expand
╭───┬────────╮
0apple
1banana
2cherry
╰───┴────────╯

If the piped data is path, you may want to use --path flag, or else manually replace the backslashes with double backslashes.

>'C:\{Users,Windows}'|str expand--path
╭───┬────────────╮
0C:\Users
1C:\Windows
╰───┴────────────╯

Brace expressions can be used one after another.

>"A{b,c}D{e,f}G"|str expand
╭───┬───────╮
0AbDeG
1AbDfG
2AcDeG
3AcDfG
╰───┴───────╯

Collection may include an empty item. It can be put at the start of the list.

>"A{,B,C}"|str expand
╭───┬────╮
0A
1AB
2AC
╰───┴────╯

Empty item can be at the end of the collection.

>"A{B,C,}"|str expand
╭───┬────╮
0AB
1AC
2A
╰───┴────╯

Empty item can be in the middle of the collection.

>"A{B,,C}"|str expand
╭───┬────╮
0AB
1A
2AC
╰───┴────╯

Also, it is possible to use one inside another. Here is a real-world example, that creates files:

>"A{B{1,3},C{2,5}}D"|str expand
╭───┬──────╮
0AB1D
1AB3D
2AC2D
3AC5D
╰───┴──────╯

Notes

This syntax may seem familiar with glob {A,B}.C. The difference is glob relies on filesystem, but str expand is not. Inside braces, we put variants. Then basically we're creating all possible outcomes.

Copyright © 2019–2023 The Nushell Project DevelopersLicensed under the MIT License.
https://www.nushell.sh/commands/docs/str_expand.html