Let's consider these 2 psql commands of 2 different difficulties:
First one - the easy:
select * from myproducts where price = 600;
Second one - the harder:
select * from myproducts where metadata @> '{"product-id": "aaa-bbb-ccc-ddd"}';
When I try to put them into a zsh script, I manage to do it for the first one, as follows:
#!/usr/bin/env zsh
#Lets take for granted I'm already connected to that db
"psql -d mycooldb -c 'select * from myproducts where price = 600;' "
Executing that script above works pretty good.
But when I try to do it with the second psql command - the harder - I never manage to handle the big quantity of quotes and my zsh script always fails (mostly syntax failures from the quotes confusion), I have something that looks like:
#!/usr/bin/env zsh
"psql -d mycooldb -c 'select * from myproducts where metadata @> '{"product-id": "aaa-bbb-ccc-ddd"}';' "
I've tried to use variables for "product-id" and its value, but (without a big surprise) it doesn't work, as well as trying to escape some quotes. I might be escaping them wrongly though.
Does anyone has a clue on how I could handle the harder psql command into a zsh script ? Thanks