(...)
introduces a subshell. So $URL
wouldn't be set after that (still have the value it had before the subshell). You want:
IFS=- read -r -a host_name_array <<< "$(hostname)" unset 'host_name_array[${#host_name_array[@]}-1]' URL="${host_name_array[*]}"
"${host_name_array[*]}"
joins the elements on the arrays on the first character of $IFS
just like "$*"
does in standard sh
.
If the reason why you're using a subshell is because you don't want to modify $IFS
globally, you could do that in a function where you give $IFS
a local scope:
f() { local IFS=- ... } f
Or use command substitution that also creates a subshell but allows passing data to the parent shell:
URL=$(IFS=-; printf '%s\n' "${host_name_array[*]}")
Here though I'd use standard sh
expansion to remove that trailing component:
URL=$(uname -n) # uname -n is the standard equivalent of hostname URL=${URL%-*}
It has several advantages over the above:
- it works even if the text contains newline characters (very unlikely for host names here though);
- if the text doesn't contain
-
, it doesn't remove anything; - it is more efficient.
read <<< $(hostname)
means running hostname
, read its output through a pipe, storing that in a temp file and have read
read the first line of that; - it doesn't clobber the variable namespace with those temporary variables
- it is shorter;
- it is portable. No need to install
bash
to run that code. The system's sh
will suffice.
In any case, remember to quote your variables when using them in list contexts:
printf '%s\n' "$URL"
When doing:
echo $URL
You're invoking the split+glob operator. So, if $IFS
still contains -
, that $URL
would be split on -
, and echo
would output those words separated by spaces.