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?
CodePudding user response:
Make it an array assignment, and then use the first element of the array (to hold with the "assuming only one match"):
foo=( /a/c*/d/ )
echo "PATH=${foo[0]}:$PATH" >>.env
By contrast, if you don't specifically want the assumption, you can use ${array[*]}
after setting :
as the first character in IFS
to expand correctly even in the case where there were multiple matches:
foo=( /a/c*/d/ )
IFS=:
echo "PATH=${foo[*]}:$PATH" >>.env
CodePudding user response:
Perhaps also
foo=( /a/c*/d/ )
echo "PATH=$(IFS=:; echo "${foo[*]}"):$PATH" >> .env
so you don't have to restore IFS