bash 脚本中的环境变量未正确显示

Environment variables not being appearing correctly in bash script

尝试添加名为 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;

替换:

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

与:

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

问题是 shell 变量没有在单引号内展开。如果要扩展 shell 个变量,请使用双引号。