1

Why does this:

arr=(1 2 4 8 16)
IFS=,
echo "${arr[*]}"

print out 1,2,4,8,16 (which is what I want) but this doesn't:

arr=(1 2 4 8 16)
IFS=, echo "${arr[*]}"

?

I'd like to have a temporary IFS without having to set it then reset/unset it.

IpsRich
  • 797
  • 10
  • 23
  • `IFS=, command eval 'echo "${arr[*]}"'` or `(IFS=,; echo "${arr[*]}")`. More info on the first one [here](https://unix.stackexchange.com/questions/92187/setting-ifs-for-a-single-statement), the second one is just creating a subshell to define the IFS for its duration – Aaron Aug 23 '19 at 13:18
  • `arr=(1 2 4 8 16); (IFS=,; echo "${arr[*]}")` – Cyrus Aug 23 '19 at 13:18
  • 3
    Order of events: the array expansion happens *before* `echo` (with its modified version of `IFS`) runs. You need to modify `IFS` for the *shell* (as in the first example), not for `echo` (which ignores its inherited value of `IFS`). – chepner Aug 23 '19 at 13:22
  • Thanks all. "Order of events" is what made it click for me. And thanks for the duplicates, @Cyrus: you're obviously better at searching than I am! – IpsRich Aug 23 '19 at 13:52

0 Answers0