Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash redirect heredoc output to /dev/null [duplicate]

I have a bash script that invokes psql and executes SQL code:

sudo -i -u postgres psql <<EOF
SQL COMMANDS;
EOF 

The SQL part generates a lot of output and I'd like to redirect it to /dev/null. How does one redirect a heredoc to /dev/null ?

like image 885
ppribeli Avatar asked Sep 16 '25 03:09

ppribeli


1 Answers

Trivially

postgres psql <<EOF >/dev/null
  SQL COMMANDS;
EOF

The << token heredoc delimiter is just another redirection operator; so you can do stuff like

postgres psql <<EOF 2>/dev/null |
  SQL COMMANDS;
EOF
while IFS= read -r output; do
    case $output in
      *error*) printf '%s%s%s\n' "$red" "$output" "$plain";;
        *) echo "$output";;
    esac
done

where the pipeline which starts on the first line continues after the EOF token. (This requires you to have defined screen control codes for red and uncolored text separately; man tput.)

like image 145
tripleee Avatar answered Sep 17 '25 18:09

tripleee