bash版本:bash-5.0.7
双引号消失了
[root@cander ~]# cat test.sh
#!/bin/bash
cat > test.conf << EOF
AA=${one:-["one","two"]}
EOF
[root@cander ~]# sh test.sh
[root@cander ~]# cat test.conf
AA=[one,two]
使用 \ 无效
[root@cander ~]# cat test.sh
#!/bin/bash
cat > test.conf << EOF
AA=${one:-[\"one\",\"two\"]}
EOF
[root@cander ~]# sh test.sh
[root@cander ~]# cat test.conf
AA=[\"one\",\"two\"]
期待内容
AA=["one","two"]
答案1
扩展内部的引用处理很奇怪。
这就是你想要的...
$ echo "${one:-["\"one\"","\"two\""]}"
["one","two"]
但这里的文档内部有所不同......
$ cat <<EOF
> ${one:-["\"one\"","\"two\""]}
> EOF
[\"one\",\"two\"]
将默认值放入变量中可能是最简单的:
$ def_one='["one","two"]'
$ cat <<EOF
> ${one:-$def_one}
> EOF
["one","two"]
答案2
你可以尝试一下这个语法:
admin@srv-web:~$ cat test.sh
#!/bin/bash
cat > test.conf << 'EOF'
AA=${one:-["one","two"]}
EOF
结果是:
administrateur@srv-web:~$ ./test.sh ; cat test.conf
AA=${one:-["one","two"]}