Reputation: 149
I have long used code similar to :
string="abc 123 xyz"
fields=( ${string} )
echo "The 3rd field = ${fields[2]}"
to break a string into fields and reference a particular element within the string.
But I ran into a strange use case today where the source string contained an '*' asterisks character. ie:
string="abc * xyz"
fields=( ${string} )
echo "The 3rd field = ${fields[2]}"
declare -p fields
In this case, the '*' does not map into the array literally, rather it appears to get expanded to a list of environment variables, and the array ends up with a much larger list or values that do not represent the original string.
Three questions:
Upvotes: 0
Views: 45
Reputation: 2615
ls *
Upvotes: 0
Reputation: 531718
Let read
do it; unquoted parameter expansions are subject to pathname expansion as well as word splitting, so *
is expanded to all file names in the current working directory.
read -a fields <<< "$string"
Alternatively, pathname expansion can also be disabled, allowing
set -f
fields=( $string )
set +f
Upvotes: 2