1

I am trying the following statement in a bash script:

su -c  "psql -d myDB-c 'SELECT count(*) AS number, date_trunc('day'::text, users.registerdate) AS registerdate FROM users;'" postgres

The Problem is the 'day' parameter which needs to be quoted, but inside the quotes it doesn't work.

tripleee
  • 175,061
  • 34
  • 275
  • 318

1 Answers1

1

You seem to be attempting to wrap single quotes inside single quotes, which of course you cannot do. 'this'and'that' parses into 'this' followed by an unquoted and followed by 'that', not into a quoted string this'and'that.

The usual solution is to wrap single quotes in double quotes, or vice versa, so "this'and'that" or 'this"and"that' if substituting double quotes inside the quoted string is acceptable; but here, you already have both, so you can't do that (straightforwardly).

Assuming psql can read commands from stdin, the simple workaround here is to use a here document.

su -c "psql -d myDB-c <<'____HERE' 
    SELECT count(*) AS number,
           date_trunc('day'::text, users.registerdate) AS registerdate
    FROM users;
____HERE" postgres
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • You could also use the "ANSI C" string syntax of Bash. See http://stackoverflow.com/questions/11966312/how-the-leading-dollar-sign-affects-single-quotes-in-bash – tripleee Feb 06 '15 at 10:28