暗无天日

=============>DarkSun的个人博客

如何在bash脚本中判断输入输出是否被重定向到文件

使用 -t FD 能判断文件描述符FD是否与终端相连,因此

if [[ -t 0 ]];then
    echo "stdin is a terminal"
else
    echo "stdin is not a terminal"
fi

if [[ -t 1 ]];then
    echo "stdout is a terminal"
else
    echo "stdout is not a terminal"
fi

if [[ -t 2 ]];then
    echo "stderr is a terminal"
else
    echo "stderr is not a terminal"
fi

stdin is not a terminal stdout is not a terminal stderr is not a terminal