环境变量在bash脚本中没有正确显示

时间:2018-02-08 06:11:07

标签: linux bash psql

尝试使用abc的名称添加数据库,但是postgresql会创建一个名为NEW_DATABASE的数据库。

export NEW_DATABASE=abc
psql -U postgres -c 'create database "$NEW_DATABASE";'

省略双引号会出错

ERROR:  syntax error at or near "$"
LINE 1: create database $NEW_DATABASE;

1 个答案:

答案 0 :(得分:1)

替换:

psql -U postgres -c 'create database "$NEW_DATABASE";'

使用:

psql -U postgres -c "create database \"$NEW_DATABASE\";"

问题是shell变量不会在单引号内扩展。如果要扩展shell变量,请使用双引号。