I have the following script in bash:
#!/bin/bash
PSQL="psql -a -e -d users -U postgres --no-align -F$'\t' -c"
FILENAME="file.ext"
QUERY="select * from users limit 1;"
${PSQL} "${QUERY}" # > ${FILENAME}
But the -F$'\t' is evaluated differently. How can we make sure that the string will not be evaluated to return a different string, i.e. finally we would like to execute the command as: psql -a -e -d users -U postgres --no-align -F$'\t' -c "select * from users limit 1;" > file.ext
The best way to solve your problem is to use arrays:
Explanation. The first line defines a new array named PSQL. You can think of it as:
In the last line, the (double-quoted) term
"${PSQL[@]}"will expand to the 10 “words” that constitute the arrayPSQLand the (double-quoted) term"${QUERY}"will expand to the stringselect * from users limit 1;considered a single word. To make things clearer, I’m going to show you how the expansion works using{and}to group each argument bash sees when expanding the line"${PSQL[@]}" "${QUERY}":Remark. Using all upper-case variable names in
bashis considered bad practice.