Assign results of globbing to a variable in Bash

The problem is that the glob will only expand if the file “rotten_eggs” exists, because it is included in the glob pattern. You should use an array.

FOO=( ryan/smells-* )
touch "${FOO[@]/%//rotten_eggs}"

The FOO array contains everything matched by the glob. The expansion using % appends /rotten_eggs to each element.

Leave a Comment