Escape single quotes when passing command to psql -c
0
votes
0
answers
754
views
I need to run the following command in a BASH shell:
sudo -u postgres /usr/pgsql-15/bin/psql -c "select count(*) from web_site where web_page like '%something%' " -d 'some_database'
The output of this command is empty, although if I paste the same query (not enclosed in double quotes) in the psql CLI, it works. So I realised that the issue is in the single quotes, because if I run the following (no single quotes in the statement):
sudo -u postgres /usr/pgsql-15/bin/psql -c "select count(*) from web_site " -d 'some_database'
It works flawlessly.
I tried the following:
sudo -u postgres /usr/pgsql-15/bin/psql -c "select count(*) from web_site where web_page like \'%something%\' " -d 'some_database'
sudo -u postgres /usr/pgsql-15/bin/psql -c 'select count(*) from web_site where web_page like \'%something%\' ' -d 'some_database'
sudo -u postgres /usr/pgsql-15/bin/psql -c "select count(*) from web_site where web_page like \"%something%\" " -d 'some_database'
sudo -u postgres /usr/pgsql-15/bin/psql -c 'select count(*) from web_site where web_page like \"%something%\" ' -d 'some_database'
sudo -u postgres /usr/pgsql-15/bin/psql -c 'select count(*) from web_site where web_page like "%something%" ' -d 'some_database'
None of these work. So how do I escape the single quotes?
Asked by enrico_steez
(11 rep)
May 25, 2023, 07:41 AM
Last activity: May 25, 2023, 07:48 AM
Last activity: May 25, 2023, 07:48 AM