我希望有人能解释一下一个现象。正如标题所示,我正在学习文件描述符的重定向。在学习过程中,我遇到了一个关于重定向符号使用的问题,特别是在设置永久重定向时。我注意到,在某些情况下,无论我使用
令我惊讶的是,在语句
<
还是 >
都似乎没有关系。例如,使用以下脚本:#!/bin/bash
#Setting file descriptor 3 to redirect to STDIN
exec 3<&0
#Setting STDIN to read from input file
exec 0<inputFile
while read var
do
echo "$var"
done
#Setting STDIN to read from file descriptor 3 which is redirected to the default STDIN
exec 0<&3
read -p "Enter a word:" word
echo "$word"
令我惊讶的是,在语句
exec 3<&0
或 exec 0<&3
中使用符号 >
或 <
似乎并不影响结果。在这些语句中,如果交换重定向符号,似乎仍然得到完全相同的结果。对我来说,显然对以下行进行相同类型的更改是可以的:exec 0<inputFile
对比。
exec 0>inputFile
由于将STDIN重定向到文件并不等同于将文件重定向到STDIN,因此不会产生相同的结果。
所以我的问题是:
<
vs>
:# Why aren't there any differences between these two statements? exec 3<&0 exec 3>&0
3<&0
vs0<&3
:# Why is there a difference between these two statements? exec 3<&0 exec 0<&3