Skip to main content
2 of 4
edited body
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

The problems you might face are the usual ones: splitting and globbing.

You can't have strings with whitespace, since they'll be split to separate array items, and anything resembling a glob character (`*?[]') will be expanded to matching filenames. Probably not a problem with host names though, but in general, it is.

This is a common issue, at repeatedly discussed, see e.g. Expansion of a shell variable and effect of glob and split on it and Security implications of forgetting to quote a variable in bash/POSIX shells


To work around those, you'd need to a) set IFS to something other than whitespace and separate your strings with that, and b) disable globbing with set -f.

(I'll get back to implementing that)

Also, like they say in the comments, you can just let the shell split your variable, or use it directly in the loop:

SRVR_ARRAY=( $HOST_NAMES )
for x in $HOST_NAMES ; do ...

(Again, you'll get trouble if HOST_NAMES contains glob characters.)

Actually, that's what is going to happen even with your example: awk prints something, the shell catches it, splits it on words, since the $() was not inside double-quotes, and then saves the words separately in the array.

ilkkachu
  • 147.9k
  • 16
  • 268
  • 441