Bash script to execute SQL command not working

I have the following SELECT statement for a PostgreSQL database to be executed from a bash script:

psql -U chh1 -d crewdb -c "SELECT SETVAL('date_link_date_linkid_seq', (SELECT MAX(date_linkid) FROM date_link), true);"

This would be the desired syntax for the command to be executed in the script. I want to replace date_link with a variable that is read from STDIN. To this end I have written the following bash script:

 1 #!/bin/bash 2 3 echo "Please enter the table name: " 4 read -r var 5 6 7 echo 'psql -U chh1 -d crewdb -c "SELECT SETVAL('"${var}"_"${var}id_seq"', (SELECT MAX("${var}id") FROM "$var"), true);"'

When I run it the SELECT MAX(.. part is not working and I get the following output:

psql -U chh1 -d crewdb -c "SELECT SETVAL(date_link_date_linkid_seq, (SELECT MAX("${var}id") FROM "$var"), true);"

Can you please help?

7 Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like