2

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?

1 Answer 1

7

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.