禁用非交互式的 .zshenv 输出 shell
Disable .zshenv output for non-interactive shell
我的 .zshenv
产生一些只有在我的 shell 是交互式的时候才有用的输出。
在其他情况下,当 shell 不是交互式时,此输出(即当我 运行 脚本时)必须隐藏。
如何抑制非交互式 shell 的 .zshenv
输出?
P.S。我在下面添加了我当前的解决方案,但它对我来说似乎很老套。
我补充了:
if [[ ! -o interactive ]]; then
exec 1>&-
exec 1<>/dev/null
fi
在我的 .zshenv
文件的顶部关闭原始 1
stdout 文件描述符并将 /dev/null
分配给它。
我在底部添加了:
if [[ ! -o interactive ]]; then
exec 1>&0
fi
恢复它(参见 )。
在某些情况下效果不佳。
我将 .zshenv
内容移动到函数 run
并为此函数输出添加了条件重定向:
function run() {
// .zshenv content
}
OUTPUT=1
if [[ ! -o interactive ]]; then
OUTPUT=3
eval "exec $OUTPUT<>/dev/null"
fi
run >& $OUTPUT
if [[ ! -o interactive ]]; then
eval "exec $OUTPUT>&-"
fi
我的 .zshenv
产生一些只有在我的 shell 是交互式的时候才有用的输出。
在其他情况下,当 shell 不是交互式时,此输出(即当我 运行 脚本时)必须隐藏。
如何抑制非交互式 shell 的 .zshenv
输出?
P.S。我在下面添加了我当前的解决方案,但它对我来说似乎很老套。
我补充了:
if [[ ! -o interactive ]]; then
exec 1>&-
exec 1<>/dev/null
fi
在我的 .zshenv
文件的顶部关闭原始 1
stdout 文件描述符并将 /dev/null
分配给它。
我在底部添加了:
if [[ ! -o interactive ]]; then
exec 1>&0
fi
恢复它(参见
我将 .zshenv
内容移动到函数 run
并为此函数输出添加了条件重定向:
function run() {
// .zshenv content
}
OUTPUT=1
if [[ ! -o interactive ]]; then
OUTPUT=3
eval "exec $OUTPUT<>/dev/null"
fi
run >& $OUTPUT
if [[ ! -o interactive ]]; then
eval "exec $OUTPUT>&-"
fi