Thread: escape character for 'psql -c ' command

escape character for 'psql -c ' command

From
Gary Fu
Date:
Hi,

How do I escape both " and ' to be used in the 'psql -c ' command ?
For example, how to make the psql command {\copy "Table" from
'txt_file'} to be used in the psql with -c option (psql -c) ?
The "Table" has to be double quoted here.

Thanks,
Gary

Re: escape character for 'psql -c ' command

From
Derrick Rice
Date:
Short answer: for simple commands, you can use shell-escaping of a double-quoted string.

psql -c "\\copy \"Table\" from 'text file'"

Note: double \\ is intentional. You need to escape the backslash, which normally escapes other special characters, like $ and ". Watch out for other special characters though, which is why I prefer the long answer...


Long answer: A *nix shell will concatenate string literals that are immediately following each other, even when they aren't the same type (single quoted or double quoted).  So the following:

"abc"'def'hij"  (reads: double quote, abc, double quote, single quote, def, single quote, double quote, hij, double quote)

is "abc" + 'def' + "hij" or "abcdefhij" to the shell

So if you have a single-quoted string, to insert a single quote you (1) stop the single quoted string (2) start a double-quoted string (3) write a single quote as the content of the double-quoted string (4) stop the double-quoted string (5) restart the single quoted string.  All without any spaces (unless they are inside either the double or single quoted strings as part of your content).

You can obviously insert 2 consecutive single quotes within a single double-quoted string - or any characters... just be aware you are in double-quotes now, so you need to escape special characters or go back to single quotes.

Your example:

psql -c ' Copy "Table" from '"'"'text file'"'"

Derrick

On Mon, Nov 15, 2010 at 6:17 PM, Gary Fu <gfu@sigmaspace.com> wrote:
Hi,

How do I escape both " and ' to be used in the 'psql -c ' command ?
For example, how to make the psql command {\copy "Table" from 'txt_file'} to be used in the psql with -c option (psql -c) ?
The "Table" has to be double quoted here.

Thanks,
Gary

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Re: escape character for 'psql -c ' command

From
Gary Fu
Date:
On 11/15/10 21:10, Derrick Rice wrote:
> Short answer: for simple commands, you can use shell-escaping of a
> double-quoted string.
>
> psql -c "\\copy \"Table\" from 'text file'"
>

This works on sh, but I tried this syntax on tcsh, it fails:

11:38am 72 gfu@modular:~/sybase2postgres> psql -c "\\copy \"Table\" from
'text file'"
Unmatched ".

How to make it work on tcsh ?

Thanks,
Gary



> Note: double \\ is intentional. You need to escape the backslash, which
> normally escapes other special characters, like $ and ". Watch out for
> other special characters though, which is why I prefer the long answer...
>
>
> Long answer: A *nix shell will concatenate string literals that are
> immediately following each other, even when they aren't the same type
> (single quoted or double quoted).  So the following:
>
> "abc"'def'hij"  (reads: double quote, abc, double quote, single quote,
> def, single quote, double quote, hij, double quote)
>
> is "abc" + 'def' + "hij" or "abcdefhij" to the shell
>
> So if you have a single-quoted string, to insert a single quote you (1)
> stop the single quoted string (2) start a double-quoted string (3) write
> a single quote as the content of the double-quoted string (4) stop the
> double-quoted string (5) restart the single quoted string.  All without
> any spaces (unless they are inside either the double or single quoted
> strings as part of your content).
>
> You can obviously insert 2 consecutive single quotes within a single
> double-quoted string - or any characters... just be aware you are in
> double-quotes now, so you need to escape special characters or go back
> to single quotes.
>
> Your example:
>
> psql -c ' Copy "Table" from '"'"'text file'"'"
>
> Derrick
>
> On Mon, Nov 15, 2010 at 6:17 PM, Gary Fu <gfu@sigmaspace.com
> <mailto:gfu@sigmaspace.com>> wrote:
>
>     Hi,
>
>     How do I escape both " and ' to be used in the 'psql -c ' command ?
>     For example, how to make the psql command {\copy "Table" from
>     'txt_file'} to be used in the psql with -c option (psql -c) ?
>     The "Table" has to be double quoted here.
>
>     Thanks,
>     Gary
>
>     --
>     Sent via pgsql-general mailing list (pgsql-general@postgresql.org
>     <mailto:pgsql-general@postgresql.org>)
>     To make changes to your subscription:
>     http://www.postgresql.org/mailpref/pgsql-general
>
>


Re: escape character for 'psql -c ' command

From
Derrick Rice
Date:
Please include the list when replying.

On Tue, Nov 16, 2010 at 11:45 AM, Gary Fu <gfu@sigmaspace.com> wrote:
Short answer: for simple commands, you can use shell-escaping of a
double-quoted string.

psql -c "\\copy \"Table\" from 'text file'"


This works on sh, but I tried this syntax on tcsh, it fails:

11:38am 72 gfu@modular:~/sybase2postgres> psql -c "\\copy \"Table\" from 'text file'"
Unmatched ".

How to make it work on tcsh ?

Consult your shell's documentation. 

http://www.tcsh.org/tcsh.html/Lexical_structure.html

http://www.tcsh.org/tcsh.html/Special_shell_variables.html#backslash_quote

Apparently you can use "set backslash_quote=1" and then the original will work.  Read the warning above about backslash_quote:

Or use the long-version, which is pretty reliable.

Derrick