1

I am trying to use the following command which has a string with single quotes inside a echo command inside a bash command:

docker exec -u postgres supre_01 bash -c 'echo \"CREATE SERVER cdu_conn FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'supre_cdu', dbname 'supre_cdu', port '5432');\" | psql -U supre_dba --dbname supre_01'

However the command outputs the following:

bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `echo \"CREATE SERVER cdu_conn FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host supre_cdu, dbname supre_cdu, port 5432);\" | psql -U supre_dba --dbname supre_01'

I have also tried:


Doubling the single quotes:

docker exec -u postgres supre_01 bash -c 'echo \"CREATE SERVER cdu_conn FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host ''supre_cdu'', dbname ''supre_cdu'', port ''5432'');\" | psql -U supre_dba --dbname supre_01'

Output:

bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `echo \"CREATE SERVER cdu_conn FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host supre_cdu, dbname supre_cdu, port 5432);\" | psql -U supre_dba --dbname supre_01'

Triple single quotes:

docker exec -u postgres supre_01 bash -c 'echo \"CREATE SERVER cdu_conn FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '''supre_cdu''', dbname '''supre_cdu''', port '''5432''');\" | psql -U supre_dba --dbname supre_01'

Output:

bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `echo \"CREATE SERVER cdu_conn FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host supre_cdu, dbname supre_cdu, port 5432);\" | psql -U supre_dba --dbname supre_01'

Single backslash:

docker exec -u postgres supre_01 bash -c 'echo \"CREATE SERVER cdu_conn FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host \'supre_cdu\', dbname \'supre_cdu\', port \'5432\');\" | psql -U supre_dba --dbname supre_01'

Output:

-bash: syntax error near unexpected token `)'

Double backslashes:

docker exec -u postgres supre_01 bash -c 'echo \"CREATE SERVER cdu_conn FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host \\'supre_cdu\\', dbname \\'supre_cdu\\', port \\'5432\\');\" | psql -U supre_dba --dbname supre_01'

Output:

bash: -c: line 0: syntax error near unexpected token `('
  • docker exec -u postgres supre_01 bash -c "echo \"CREATE SERVER cdu_conn FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'supre_cdu', dbname 'supre_cdu', port '5432');\" | psql -U supre_dba --dbname supre_01" – muru Apr 12 '22 at 14:54
  • But now that I think of it, you don't even need this extra quoting - just move the pipe out of docker: echo "CREATE SERVER cdu_conn FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'supre_cdu', dbname 'supre_cdu', port '5432');" | docker exec -u postgres supre_01 psql -U supre_dba --dbname supre_01 – muru Apr 12 '22 at 16:04

0 Answers0