Unix-标准输入和输出

问题描述 投票:0回答:1
$(($one/$zero)) 2> tasty.txt >> cat.txt
$(($one/$zero)) 2> tasty.txt >> cat.txt

我的输入

one=1

zero=0

为什么错误没有被重定向到tasty.txt,而是被打印出来?

unix stream
1个回答
0
投票

因为当您在$()中执行命令时,它将打开一个新的子shell。

:-:echo "one is $one and Zero is $zero" 
one is 1 and Zero is 0
:-:eval $one/$zero 2> error.txt
:-:cat error.txt
bash: 1/0: No such file or directory

当我在当前Shell中执行命令时,错误将重定向到文件。

:-:$($one/$zero) 2> error_1.txt
bash: 1/0: No such file or directory
:-:cat  error_1.txt 

当我在subshel​​l中执行命令时,错误将重定向到标准输出。

:-:$($one/$zero 2> error_1.txt) 
:-:cat error_1.txt 
bash: 1/0: No such file or directory
:-:

但是当我在子shell内的文件中重定向标准错误时,它将被捕获。

© www.soinside.com 2019 - 2024. All rights reserved.