I want to execute a psql statement within a bash script and output the results to a file. The code I have below works as desired:
#!/bin/bash
query=\"select
You are overwriting the file each time with >
inside the loop. You need >>
inside or have >
outside the loop:
#!/bin/bash
query="select * from mytable;"
for (( i=0; i<5; i++ ))
do
psql < output.txt
Putting >
after done
is a little more efficient than >>
inside the loop.
Similar post: