Paolo
Paolo

Reputation: 15827

Append output from both stdout and stderr of every command of a bash script to file

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

Upvotes: 1

Views: 17702

Answers (1)

anubhava
anubhava

Reputation: 784898

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

Upvotes: 3

Related Questions