Skip to main content
Missing --
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

If you want to redirect stdout to be appended to a file, and not also have a copy of the output on the terminal, just do:

exec >> "$OUT"

The only purpose of tee is to duplicate one stream into multiple, so if you only want the output in one place, there's no need for it. Your tee -ia "$OUT" > /dev/null would of course also work, but that's a bit silly: you'd still be duplicating the output, just to discard the other copy.

I'm unsure about the use of curly braces {} in this context

Braces require a semicolon, or a newline. It actually says that in the manual: "The semicolon (or newline) following list is required." Parenthesis require neither.

The implications of a subshell are that the subshell runs in an independent copy of the shell, and any changes in the subshell are not visible outside it. If the script ends anyway when the subshell ends, that doesn't mean much, and all the subshell does is start an unneeded process.

I would go with something like

if [ "$TERM_OPT" != "OFF" ]; then
     exec > >(tee -ia -- "$OUT")
else
     exec >> "$OUT"
fi

If you want to redirect stdout to be appended to a file, and not also have a copy of the output on the terminal, just do:

exec >> "$OUT"

The only purpose of tee is to duplicate one stream into multiple, so if you only want the output in one place, there's no need for it. Your tee -ia "$OUT" > /dev/null would of course also work, but that's a bit silly: you'd still be duplicating the output, just to discard the other copy.

I'm unsure about the use of curly braces {} in this context

Braces require a semicolon, or a newline. It actually says that in the manual: "The semicolon (or newline) following list is required." Parenthesis require neither.

The implications of a subshell are that the subshell runs in an independent copy of the shell, and any changes in the subshell are not visible outside it. If the script ends anyway when the subshell ends, that doesn't mean much, and all the subshell does is start an unneeded process.

I would go with something like

if [ "$TERM_OPT" != "OFF" ]; then
     exec > >(tee -ia "$OUT")
else
     exec >> "$OUT"
fi

If you want to redirect stdout to be appended to a file, and not also have a copy of the output on the terminal, just do:

exec >> "$OUT"

The only purpose of tee is to duplicate one stream into multiple, so if you only want the output in one place, there's no need for it. Your tee -ia "$OUT" > /dev/null would of course also work, but that's a bit silly: you'd still be duplicating the output, just to discard the other copy.

I'm unsure about the use of curly braces {} in this context

Braces require a semicolon, or a newline. It actually says that in the manual: "The semicolon (or newline) following list is required." Parenthesis require neither.

The implications of a subshell are that the subshell runs in an independent copy of the shell, and any changes in the subshell are not visible outside it. If the script ends anyway when the subshell ends, that doesn't mean much, and all the subshell does is start an unneeded process.

I would go with something like

if [ "$TERM_OPT" != "OFF" ]; then
     exec > >(tee -ia -- "$OUT")
else
     exec >> "$OUT"
fi
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

If you want to redirect stdout to be appended to a file, and not also have a copy of the output on the terminal, just do:

exec >> "$OUT"

The only purpose of tee is to duplicate one stream into multiple, so if you only want the output in one place, there's no need for it. Your tee -ia "$OUT" > /dev/null would of course also work, but that's a bit silly: you'd still be duplicating the output, just to discard the other copy.

I'm unsure about the use of curly braces {} in this context

Braces require a semicolon, or a newline. It actually says that in the manual: "The semicolon (or newline) following list is required." Parenthesis require neither.

The implications of a subshell are that the subshell runs in an independent copy of the shell, and any changes in the subshell are not visible outside it. If the script ends anyway when the subshell ends, that doesn't mean much, and all the subshell does is start an unneeded process.

I would go with something like

if [ "$TERM_OPT" != "OFF" ]; then
     exec > >(tee -ia "$OUT")
else
     exec >> "$OUT"
fi