I'm trying to convert the output of a command like echo -e "ab\\nc\\nd e"
to an array.
X=( $(echo -e "a b\nc\nd e") )
Splits the input for every new line and whitespace character:
$ echo ${#X[@]}
> 5
for i in ${X[@]} ; do echo $i ; done
a
b
c
d
e
The result should be:
for i in ${X[@]} ; do echo $i ; done
a b
c
d e
You need to change your Internal Field Separator variable ( IFS
) to a newline first.
$ IFS=$'\n'; arr=( $(echo -e "a b\nc\nd e") ); for i in ${arr[@]} ; do echo $i ; done
a b
c
d e
readarray -t "[array]" <<< "$([command])"
Set the IFS
to newline
. By default, it is space
.
[jaypal:~] while IFS=$'\n' read -a arry; do
echo ${arry[0]};
done < <(echo -e "a b\nc\nd e")
a b
c
d e
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.