简体   繁体   中英

Shell script: redirect output

Our shell script contains the header

#!/bin/bash -x

that causes the commands to also be listed. Instead of having to type

$ ./script.sh &> log.txt

I would like to add a command to this script that will log all following output (also) to a log file. How this is possible?

You can place this line at the start of your script:

# redirect stdout/stderr to a file
exec &> log.txt

EDIT: As per comments below:

#!/bin/bash -x

# redirect stdout/stderr to a file and still show them on terminal
exec &> >(tee log.txt; exit)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM