我的脚本是这样的:
#!/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
Run Code Online (Sandbox Code Playgroud)
结果是这样的:
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
Run Code Online (Sandbox Code Playgroud)
我想将 bash 变量设置output为 mysql 查询的输出,但该output变量似乎很好,除了所有新行都被删除了。我的问题是如何将bash变量设置为一行一行的mysql查询结果?
代替
echo $output
Run Code Online (Sandbox Code Playgroud)
引用它!了解分词
echo "$output"
Run Code Online (Sandbox Code Playgroud)
“双引号”包含空格/元字符和每个扩展的每个文字:"$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