Thread: psql -c command parse "select $$hello$$" failed

psql -c command parse "select $$hello$$" failed

From
jian he
Date:
Hi.
not sure this is the expected result.

/home/jian/postgres/pg16_test/bin/psql -d test_dev -p 5455 -c "select $$hello$$"
2023-07-04 20:15:51.066 CST [1562050] ERROR:  trailing junk after
numeric literal at or near "884070h" at character 8
2023-07-04 20:15:51.066 CST [1562050] STATEMENT:  select 884070hello884070
ERROR:  trailing junk after numeric literal at or near "884070h"
LINE 1: select 884070hello884070
               ^



Re: psql -c command parse "select $$hello$$" failed

From
Julien Rouhaud
Date:
Hi,

On Tue, Jul 04, 2023 at 08:21:10PM +0800, jian he wrote:
> Hi.
> not sure this is the expected result.
>
> /home/jian/postgres/pg16_test/bin/psql -d test_dev -p 5455 -c "select $$hello$$"
> 2023-07-04 20:15:51.066 CST [1562050] ERROR:  trailing junk after
> numeric literal at or near "884070h" at character 8
> 2023-07-04 20:15:51.066 CST [1562050] STATEMENT:  select 884070hello884070
> ERROR:  trailing junk after numeric literal at or near "884070h"
> LINE 1: select 884070hello884070
>                ^

That's expected, $ is used for variable in the shell and is the pid of the
current process.  You have to escape any $ in a double-quoted string, or use a
single quoted strings.



Re: psql -c command parse "select $$hello$$" failed

From
Erik Wienhold
Date:
> On 04/07/2023 14:21 CEST jian he <jian.universality@gmail.com> wrote:
>
> not sure this is the expected result.
>
> /home/jian/postgres/pg16_test/bin/psql -d test_dev -p 5455 -c "select $$hello$$"
> 2023-07-04 20:15:51.066 CST [1562050] ERROR:  trailing junk after
> numeric literal at or near "884070h" at character 8
> 2023-07-04 20:15:51.066 CST [1562050] STATEMENT:  select 884070hello884070
> ERROR:  trailing junk after numeric literal at or near "884070h"
> LINE 1: select 884070hello884070

The error is expected because the shell replaces $$ with its process ID inside
double quoted strings.  Check out Bash quoting [1] (assuming that you use bash
but this applies to every(?) shell).

Either use single quotes around the statement or escape every $ with \$:

    psql -c 'select $$hello$$'
    psql -c "select \$\$hello\$\$\"

[1] https://www.gnu.org/software/bash/manual/bash.html#Quoting

--
Erik