How to get line count from variable (from MYSQL query)?

Posted by Mint on Stack Overflow See other posts from Stack Overflow or by Mint
Published on 2010-03-14T04:54:50Z Indexed on 2010/03/14 5:05 UTC
Read the original article Hit count: 113

Filed under:
|
|
|

My problematic code:

testMYSQL=`mysql -u $mysqlUser -p$mysqlPass -h $mysqlHost --skip-column-names --batch -D $mysqlDB -e "SELECT $select FROM $mysqlTable WHERE nameTXT='test';"`       

$testMYSQL now contains:

test
test
test

Then I do:

TEST=$(echo $testMYSQL | wc -l)
echo "$TEST"

I would of thought that would work, but it doesn't, it returns 1

But if I put this into $testMYSQL: "test\ntest\ntest" it will say 3…

Whats going on here? does MYSQL not use new lines?

PS, I know I can use a for loop to loop though the lines then count up the lines that way, but I was hoping for a simpler solution like wc

© Stack Overflow or respective owner

Related posts about linux

Related posts about bash