Shell script - psql run multiple statements from a variable one by one in one transaction Im preparing a shell script to generate analyze command and all the commands will go to a variable.

echo $analyze_query

analyze tbl1;
analyze tbl2;
analyze tbl3;


Now I want to run one by one, and Im able to achieve that by using the below command.

for $i in $analyze_query
do
psql -h host -U user -d db -c"$i"
done


But this query will close and open a new connection for every command, Is there any way that I can run the one by one but in one session?
Yes, we can do it. 


echo the variable and then pipe it to psql.


echo "$analyze_query"  |  psql -h host -U user -d db 


Use () to group the command output then pipe it to psql.


(for i in $analyze_query 
do
 echo $i
done ) |  psql -h host -U user -d db