mysql 结果到 bash 变量

mysql 结果到 bash 变量

我的脚本是这样的:

#!/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

结果是这样的:

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

我想将 bash 变量设置output为 mysql 查询的输出,但output除了所有新行都被删除之外,该变量似乎很好。我的问题是如何将 bash 变量设置为 mysql 查询结果,每一行都在一行中?

答案1

代替

echo $output

引用一下吧!阅读分词

echo "$output"

“双引号”包含空格/元字符的每个文字以及每一个扩张:"$var""$(command "$var")""${array[@]}""a & b"。用于'single quotes'代码或文字$'s: 'Costs $5 US'ssh host 'echo "$HOSTNAME"'.看
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words

相关内容