help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

split a string into an array?


From: Peng Yu
Subject: split a string into an array?
Date: Thu, 10 Mar 2022 21:25:53 -0600

Hi,

I currently use the following method to split a string into an array
using a separator (TAB is used in the example, but it could be other
characters). Is this a robust way to do so? Is there any corner case
that it will not handle correctly?

$ readarray -t -d $'\t' array <<< $'a\t\tb\tc'
$ array[-1]=${array[-1]%$'\n'}
$ declare -p array
declare -a array=([0]="a" [1]="" [2]="b" [3]="c")

Also, when the separator is not a single character but rather a regex,
is there a good way to split a string into an array? Thanks.

-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]