我正在尝试使用多个引号执行命令,但似乎有问题:
sudo su - postgres -c 'psql -U postgres -c "alter user postgres with password 'dummy';"'
ERROR: syntax error at or near "dummy"
LINE 1: alter user postgres with password dummy;
^
Run Code Online (Sandbox Code Playgroud)
如果我分两步执行相同的命令,没有问题:
sudo su - postgres
postgres@courgette:~$ psql -U postgres -c "alter user postgres with password 'dummy';"
ALTER ROLE
Run Code Online (Sandbox Code Playgroud)
如何在一行中首先正确引用"虚拟"?
用单引号('
'')括起字符可以保留引号中每个字符的字面值.单引号之间可能不会出现单引号,即使前面有反斜杠也是如此.
所以,你有三个合理的选择:
您可以退出单引号位,只需添加双引号单引号:
sudo su - postgres -c 'psql -U postgres -c "alter user postgres with password '"'"'dummy'"'"';"'
Run Code Online (Sandbox Code Playgroud)你可以使用双引号"...",在这种情况下你必须转义任何内部双引号:
sudo su - postgres -c "psql -U postgres -c \"alter user postgres with password 'dummy';\""
Run Code Online (Sandbox Code Playgroud)你可以使用$'...'(它与两者不同,'...并且"..."它支持各种C风格的转义序列),在这种情况下你必须转义任何内部单引号:
sudo su - postgres -c $'psql -U postgres -c "alter user postgres with password \'dummy\';"'
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
699 次 |
| 最近记录: |