Sujet : Re: Splitting in shell (bash)
De : Lem (at) *nospam* none.invalid (Lem Novantotto)
Groupes : comp.unix.shellDate : 10. Nov 2024, 01:51:58
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vgp03e$iqq$1@dont-email.me>
References : 1
User-Agent : Pan/0.160 (Toresk; )
Il Sat, 9 Nov 2024 16:19:17 -0000 (UTC), Kenny McCormack ha scritto:
First note/caveat: I'm not interested in any solution involving IFS, for
two reasons:
1) IFS-based solutions never work for me.
2) Changing IFS is inherently dangerous, because, well, IFS itself
inherently dangerous. Yes, I know it has been somewhat de-fanged
recently - but it is still dangerous.
Sorry to bother you, but... could you please give me some hints? Why
dangerous? Thanks. :-)
Anyway, the point of this thread is that I have recently developed a
good solution for this, using bash's "mapfile" command.
[...]
mapfile -td ';' <<< "$foo"
Yes, mapfile is a good solution. Of course, when you use <<<, no word
expansion is performed, so IFS is out of the question.
Caveats:
1) You can only have one, single character delimiter.
But you could have more using IFS. Something like:
| $ IFS=";:[ENTER]
| "
| $ declare myarray
| $ for i in $(echo "one:two;three 3[ENTER]
| four"); do myarray+=( $i ); done
| $ echo ${myarray[@]}
| one two three 3 four
| $ echo ${myarray[2]}
| three 3
What would be wrong with it?
-- Bye, Lem