Store grep output containing spaces in an array

I want to save some blkid output blkid in an array. The problem is that these lines contain spaces, and the array syntax accepts them as separators for individual elements of the array, so I get broken lines in my array, and not one line having one array element.

This is the code I have: devices=($(sudo blkid | egrep '^/dev/sd[bz]'))

echo ${devices[*]} gives me the following result:

 /dev/sdb1: LABEL="ARCH_201108" TYPE="udf" /dev/sdc1: LABEL="WD" UUID="414ECD7B314A557F" TYPE="ntfs" 

But echo ${#devices[*]} gives me 7 , but insted I want to have 2 . I want /dev/sdb1: LABEL="ARCH_201108" TYPE="udf" be the first element in my device array, and /dev/sdc1: LABEL="WD" UUID="414ECD7B314A557F" TYPE="ntfs" - second . How can i do this?

+6
source share
1 answer

The elements of the array are separated by the IFS value. If you want to split to a new line, configure IFS:

 IFS_backup=$IFS IFS=$'\n' devices=($(sudo blkid | egrep '^/dev/sd[bz]')) IFS=$IFS_backup echo ${#devices[@]} 
+13
source

Source: https://habr.com/ru/post/905725/


All Articles