typeset -ga aaa=("
abc
def ghi
jkl mno
pqr
")
This makes aaa an array with a grand total of 1 element. Then you do this:
Your prefix text is wrong; calling $# a "linecount" is a gross mischaracterization. It never ever ever counts lines. Never. It counts characters on a scalar variable, elements of an array. Lines have nothing to do with it.redline "linecount of aaa is: $#aaa ... Single element!"
ccc=( ${(@f)aaa} )
Now you're keeping the blank lines, so the (f) gives you a separate array element for every line that you get from the expansion - 8 of them, the same as the output of print -l $aaa | wc -l.ddd=( "${(@f)aaa}" )
redline '\nddd=( "${(@f)aaa}" ) ... it seems like a lot of trouble to copy the array as it is.'
eee=( $=aaa )
A script:I dunno. I've lost the example that I saved. Never mind, I'll keep an eye on it, and figure out next time. Red herring for now. It's a bit confusing looking into typeset -p output. All this 'splitting' stuff is not simple. For now everything works and I'm quite sure it's a bit more orthodox.
---------------------------------------------------------------------------------
redline () { echo -e "$red$@$nrm" }
typeset -ga aaa=("
abc
def ghi
jkl mno
pqr
")
redline "\naaa:"
print -l $aaa
redline "\naaa[1]:"
print -l $aaa[1]
redline "linecount of aaa is: $#aaa ... Single element!"
ccc=( ${(@f)aaa} )
redline '\nccc=( ${(@f)aaa} )'
print -l $ccc
redline "linecount of ccc is: $#ccc ... split, but blank lines gone."
ddd=( "${(@f)aaa}" )
redline '\nddd=( "${(@f)aaa}" ) ... it seems like a lot of trouble to copy the array as it is.'
print -l "$ddd[@]" #... don't forget the quotes, idiot!"
redline "linecount of ddd is: $#ddd ... split, but blanks preserved and counted. NB EIGHT NOT SIX!"
redline "\nddd[1]: That's right it's blank, just as it should be."
print -l $ddd[1]
redline "\nddd[2]:"
print -l $ddd[2]
redline "And now the typesets\n"
typeset -p aaa
typeset -p ccc
typeset -p ddd
# Verbatim:
# typeset -a aaa=( $'\nabc\n\n\tdef ghi\n\tjkl mno\n\n\tpqr\n' )
# typeset -a ccc=( abc $'\tdef ghi' $'\tjkl mno' $'\tpqr' )
# typeset -a ddd=( '' abc '' $'\tdef ghi' $'\tjkl mno' '' $'\tpqr' '' )
# Aligned: Note the steeenking dollars ;-) ... and how they seem to replace the newlines. No?
# typeset -a aaa=( $'\nabc \n\n\tdef ghi \n\tjkl mno \n\n\tpqr\n' ) #Original with blanks
# typeset -a ccc=( abc $'\tdef ghi' $'\tjkl mno ' $'\tpqr' ) #No blanks.
# typeset -a ddd=( '' abc '' $'\tdef ghi' $'\tjkl mno' '' $'\tpqr' '' ) #Blanks retained BUT not the same structure!
-----------------------------------------------------------------------------------
... so what about the dollars?
Anyway, it comes clear: '${(@f)....}' doesn't ADD (or retain) newlines it removes them! (Tho splitting the array where they used to be.) Doesn't need them cuz elements aren't separated by newlines (like I thought) but by ... however it's done. Single quotes indicate elements. And, best of all, copying an array, blanks and all, (not shown above) turns out to be simple:
eee=( $aaa ) and:
eee=( "{$(@f)aaa}" )
... turns out to be a false equivalence where newlines are REMOVED. Don't be fooled by 'printf' -- it might not show you the genuine state of the array. Scalars show their blank lines automatically but we need:
print -l -- "$eee[@]"
... with arrays. Trust typedef -p.