Expanding item={one,two,three} with a wildcard

You can store the current directory’s files in an array as follows:

items=(*)

Then parameter expansion can be used to add the prefix:

printf "%s\n" "${items[@]/#/item=}"

You can use this to update the array:

items=("${items[@]/#/item=}")

With zsh:

items=(*)
print -rC1 item=$^items

Or:

(){ print -rC1 item=$^@; } *

Or:

(){ print -rC1 ${@/#/item=}; } *

Or:

print -rC1 ./*(:s:./:item=)

Or:

set -o histsubstpattern
print -rC1 *(:s/#/item=)

Or:

print -rC1 *(e['REPLY=item=$REPLY'])

Or:

printf '%s%s\n' *(P[item=])

Or:

items=()
printf -v items 'item=%s' *
print -rC1 -- $items

etc.

For all of those add the N glob qualifier to get no output (or an empty line with the printf one) instead of an error if there's no matching file (here with *, that would be if the current directory is not readable or contains only hidden files).

Of course, if it's just about printing them, then you can just do (in any shell):

printf 'item=%s\n' *

Tags:

Bash

Wildcards