In bash, why isn't this:
declare -a array=(a b c d e)
IFS=',' echo "${array[*]}"
the same as:
echo_wrapper()
{
echo "$*"
}
declare -a array=(a b c d e)
IFS=',' echo_wrapper "${array[@]}"
Expansion happens before variable assignments within a simple command. From the Bash manual:
When a simple command is executed, the shell performs the following expansions, assignments, and redirections, from left to right.
- The words that the parser has marked as variable assignments (those preceding the command name) and redirections are saved for later processing.
- The words that are not variable assignments or redirections are expanded (see Shell Expansions). If any words remain after expansion, the first word is taken to be the name of the command and the remaining words are the arguments.
- Redirections are performed as described above (see Redirections).
- The text after the ‘=’ in each variable assignment undergoes tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal before being assigned to the variable.
Step 1 identifies your IFS assignment, but doesn't perform it. Step 2 expands "${array[*]}" using the existing value of IFS. Then step 4 changes the value of IFS.
Workaround, if you don't need to modify the process environment, working directory, etc.: use a subshell.
(IFS=, ; echo "${array[*]}")
a different workaround
join () {
local IFS=$1 # changes to IFS are localized to the function
shift
echo "$*"
}
join , "${array[@]}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With