1

Here

https://stackoverflow.com/a/876267/1579327

I learned how to do that for a single command cmd appending output to file.txt

cmd >>file.txt 2>&1

But my script contains many statements and commands.

And I would like to avoid appending >>file.txt 2>&1 to every line.

Is there a directive to let me to do that by default for every subsequent command?


Side note: I'm looking for a solution suitable also for MacOs X's bash

Community
  • 1
  • 1
Paolo
  • 15,233
  • 27
  • 70
  • 91

1 Answers1

3

On top of your script you can use exec like this:

#!/bin/bash

# append stdout/stderr to a file
exec >> file.log 2>&1

# script starts here
anubhava
  • 761,203
  • 64
  • 569
  • 643