Loading...

Splitting string to array in bash with long delimiter

Answer #1 100 %

IFS can split string on each of the single character only and when you provide

it splits input on <, b, r and >.

You can use this work-around to get your job done:

s="FIRST

SECOND

THIRD" del=$'\5' # our arbitrary delimiter; can be any other control character IFS="$del" read -ra arr <<< "${s//

/$del}" declare -p arr

declare -a arr='([0]="FIRST" [1]="SECOND" [2]="THIRD")'
  • del=$'\5' sets delimiter to control character \5. \5 is just an arbitrary character and it can be replaced with any other character that is not present in input.
  • "${s//

    /$del}"
    replace

    with control character \5
  • IFS="$del" splits input on \5 while populating array












You’ll also like:


© 2022 CodeForDev.com -