0

My script is like this:

#!/bin/bash

mysql dbname -N -uroot -p -hhost -se "SELECT COUNTRY, COUNTRY_NAME FROM server ORDER BY COUNTRY"

output=$(mysql dbname -N -uroot -p -hhost -se "SELECT COUNTRY, COUNTRY_NAME FROM server ORDER BY COUNTRY")
echo $output

The result is like this:

Enter password: 
CN  China
CZ  Czech Republic
FI  Finland
JP  Japan
NL  Netherlands
RO  Romania
SG  Singapore
UA  Ukraine
US  United States
US  United States
US  United States
US  United States
Enter password: 
CN China CZ Czech Republic FI Finland JP Japan NL Netherlands RO Romania SG Singapore UA Ukraine US United States US United States US United States US United States

I want to set bash variable output to be the output of the mysql query, but the output variable seems to be good except all new lines were stripped away. My question is how to set the bash variable to be the mysql query result with each row in one line?

5

Instead of

echo $output

quote it ! Read about word splitting

echo "$output"

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.