`exec < somefile` 的行為與 `source somefile` 相同嗎

`exec < somefile` 的行為與 `source somefile` 相同嗎
$ cat somefile 
sleep 100
$ exec < somefile 
$ sleep 100

除了其呼叫進程在完成後終止之外,其行為是否exec < somefile與其他相同?source somefile

謝謝。

答案1

效果相似,但行為不嚴格相同。

exec < somefile將標準輸入連接到somefile,因此 shell 開始從該檔案讀取,而不是從目前連接的終端讀取。 (嗯,至少 bash 是這樣,Zsh 的行為有所不同。)實際上,這會導致當前 shell 處理 的內容somefile,就像處理 一樣source somefile。一旦耗盡somefile,shell 就會退出。

相關內容