Bash Wildcards – Expanding Item with Curly Braces

bashwildcards

If I use bash's brace expansion I get a list

echo item={one,two,three}
item=one item=two item=three

Assuming I am in a directory with files/folders that would match a wildcard, is there any way to have an expansion that matches these files/folders?

ls
blue  green  red

echo item=*      # Obviously not
item=*

echo item={*}    # Maybe? ...but no
item={*}

In my example I would like the expansion to be item=blue item=green item=red

The best I've got so is code like this

items=()
for dirent in *; do items+=("item=$dirent"); done
echo "${items[@]}"

item=blue item=green item=red

Best Answer

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=}")
Related Question