Search code examples
fish

Iteration through letters in fish shell


In bash I can:

for i in /dev/sd{b..g}; do pvcreate $i; done

Looking for the equivalent of this loop in fish.

for i in /dev/sd{b..g}
    pvcreate $i
end

Returns Device /dev/sdb..g not found (or ignored by filtering). error


Solution

  • There's no direct translation: fish only does comma-separated elements inside braces -- https://fishshell.com/docs/current/index.html#expand-brace

    You can't do for i in /dev/sd[b-g] either because fish does not use character sets as wildcards in globbing -- https://fishshell.com/docs/current/index.html#expand-wildcard

    You're ending up calling some external program to generate that list of files, and you know bash works, so:

    for i in (bash -c 'printf "%s\n" /dev/sd{b..g}'); ...
    

    This is an example of one of fish's design principles:

    Everything that can be done in other shell languages should be possible to do in fish, though fish may rely on external commands in doing so.