简体   繁体   English

Bash:执行带引号的命令并将其输出放入变量中

[英]Bash: execute a command with quotes and put its output into a variable

In a bash script I set a command this way:在 bash 脚本中,我以这种方式设置了一个命令:

getdblist_cmd=(sudo -u $backup_user $psql -p $pgsql_port -U $pgsql_user -d postgres -q -t -c 'SELECT datname from pg_database')

Then I run it with然后我运行它

dblist=`${getdblist_cmd[@]}`

I don't get any error into the bash script but the $dblist variable is empty.我没有在 bash 脚本中收到任何错误,但$dblist变量为空。

How can I execute that command and put its output into a variable?如何执行该命令并将其输出放入变量中?

dblist=`"${getdblist_cmd[@]}"`
        ^                   ^

The [@] operator works only correct if the ${...} is surrounded by quotes. [@]运算符仅在${...}被引号包围时才正确工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM