1

I'm trying to output a,b,c,

This does not work:

a=(a b c)
IFS=, echo "${a[*]}"

But this works:

a=(a b c)
IFS=,
echo "${a[*]}"

Anyone know why the first one does not work?

daisy
  • 54,555

1 Answers1

1

This is because the variable is expanded before the new value of IFS is actually set. This is described at https://www.gnu.org/software/bash/manual/bash.html#Simple-Command-Expansion

Use a subshell to set the value without affecting the current shell:

(IFS=,; echo "${a[*]}")
glenn jackman
  • 85,964