Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store grep output containing whitespaces in an array

I want to store some lines of the output of blkid in an array. The problem is, that those lines contain whitespace and the array syntax takes those as delimiters for single array elements, so that i end up with splitted lines in my array instead of one line beeing one array element.

This is the code i currently have: devices=($(sudo blkid | egrep '^/dev/sd[b-z]'))

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

/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" to be the first element in my devices array and /dev/sdc1: LABEL="WD" UUID="414ECD7B314A557F" TYPE="ntfs" to be the second one. How can i accomplish that?

like image 341
Alexander Baier Avatar asked Jan 11 '12 15:01

Alexander Baier


1 Answers

Array elements are split on the IFS value. If you want to split on newline, adjust IFS:

IFS_backup=$IFS
IFS=$'\n'
devices=($(sudo blkid | egrep '^/dev/sd[b-z]'))
IFS=$IFS_backup
echo ${#devices[@]}
like image 96
choroba Avatar answered Oct 20 '22 01:10

choroba