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?
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[@]}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With