DEV Community

Will Sheppard
Will Sheppard

Posted on • Edited on

Escape quotes correctly when using psql via docker in bash

docker compose exec site bash -c 'PGPASSWORD=***** psql -Ufoo -d bar \
  -h example.com -x -c "select qux, date(xyzzy) from tabley          \
  where field in(1, 2, 3) and that_time > '"'"'2024-11-18'"'"'       \
  order by that_time;"'
Enter fullscreen mode Exit fullscreen mode

Explanation:

'"'"' is the Quoteception operator.

When used in the middle of a single quoted string, it:

  • ' Ends the single quoted string
  • " Starts a new double quoted string
  • ' Containing only one single quote
  • " Ends the double quoted string
  • ' Starts the next single quoted string

Or described another way:

echo 'Embedded single quote is here: '"'"' did you see it?'
Enter fullscreen mode Exit fullscreen mode

Or even:

echo 'Embedded single quote is here: ' \
"'"                                    \
' did you see it?'
Enter fullscreen mode Exit fullscreen mode

Outputs:

Embedded single quote is here:  '  did you see it?
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay