Say I have the following in Bash:
FOO=/a/c*/d/
echo PATH=${FOO}:${PATH} >> .env
When I inspect the contents of the file .env the path still contains the wildcard. How can I ensure that the wildcard is expanded to the path, assuming there is only one match, so that the wildcard does not exist in the file when echoed?
Make it an array assignment, and then use the first element of the array (to hold with the "assuming only one match"):
By contrast, if you don't specifically want the assumption, you can use
${array[*]}after setting:as the first character inIFSto expand correctly even in the case where there were multiple matches:You could avoid needing to change
IFSor pay the efficiency cost of starting a subprocess by expanding the list of paths to a separate variable: