Why doesn't Bash overwrite my array with a new one? -


i stripped use case these 3 lines (plus output):

a=(foo bar) a=$(echo "spam egg") echo ${a[@]} spam egg bar 

it creates array 2 element written hand. time later want replace array output command line tool (e.g. ls *.vhd). instead of replacing array, bash replaces first element, bar gets "attached" @ end.

if use handwritten array, can't reproduce behavior.

a=(foo bar) a=(spam egg) echo ${a[@]} spam egg 

so suspect it's related using $(). how can solve problem?

use parentheses in assignment, treated array, rather one long string spaces:

a=$(echo "spam egg") echo ${a[0]} 

-> spam egg

a=($(echo "spam egg")) echo ${a[0]} 

-> spam

(the parentheses $() tell bash run command don't count telling collect result array.)


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -