[英]Why does "exit" not exit my script when I pipe it to "tee"?
我正在運行一個腳本,該腳本使用tee
將 output 記錄到屏幕和文件中:
{ echo "hello world" ; exit 1; } | tee -a logfile.log
exit
不工作。 我無法退出我的腳本。 當我刪除tee
時,它可以工作。 這是為什么?
如其他地方所述(包括,在某種程度上間接地, BashFAQ #24 ),管道創建子shell,因此您的exit
僅退出子shell。
您可以通過使用重定向到進程替換來替換管道來避免這種情況:
{ echo "hello world"; exit 1; } > >(tee -a logfile.log)
發生這種情況是因為管道為每個組件生成了一個子外殼,因此exit
在這個新的 shell 內運行。
您應該像這樣重寫腳本:
echo "hello world" | tee -a logfile.log
exit 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.