【发布时间】:2023-03-03 14:47:01
【问题描述】:
我的意图是让我的 bash 脚本的所有输出显示在控制台上并记录到一个文件中。
这是我的脚本,按预期工作。
#!/bin/bash
LOG_FILE="test_log.log"
touch $LOG_FILE
# output to console and to logfile
exec > >(tee $LOG_FILE) 2>&1
echo "Starting command ls"
ls -al
echo "End of script"
但是我不明白为什么它会这样工作。
我希望exec >>(tee $LOG_FILE) 2>&1 可以工作,但它失败了,尽管exec >>$LOG_FILE 2>&1 确实可以工作。
我在bash manual 和advanced bash scripting 中都找不到构造exec > >(command ) 的原因。你能解释一下它背后的逻辑吗?
【问题讨论】:
标签: bash exec io-redirection