在while循环中重定向stdin的bash命令(使用sed)

在while循环中重定向stdin的bash命令(使用sed),bash,while-loop,stdin,readfile,Bash,While Loop,Stdin,Readfile,我正在尝试从文件(output.txt)到while循环中仅标准输入从1到1000的行 我试过这样的方法: #!/bin/bash while read -r line; do echo "$line" done < (sed -n 1,1000p data/output.txt) #/bin/bash 而read-r行;做 回音“$line” 完成

我正在尝试从文件(output.txt)到while循环中仅标准输入从1到1000的行

我试过这样的方法:

#!/bin/bash
while read -r line; do
    echo "$line"
done < (sed -n 1,1000p data/output.txt)
#/bin/bash
而read-r行;做
回音“$line”
完成<(sed-n 11000p数据/output.txt)
刚刚尝试过:

#!/bin/bash
while read -r line; do
    echo "$line"
done < <(sed -n 1,1000p data/output.txt)
#/bin/bash
而read-r行;做
回音“$line”
完成部件
myfifo&
而read-r行;做
回音“$line”
完成
您似乎希望通过管道将输出从一个命令传递到另一个命令。 如果是,请使用管道:

sed -n 1,1000p data/output.txt | while read -r line; do echo "$line"; done
或者,为正确的作业使用正确的工具:

head -1000 data/output.txt | while read -r ; do something; done

搜索
bash
并显式设置解释器是明智的,因为您在
bash
中以
#的形式运行它/bin/bash
或安装在机器中的任何位置这种方法的问题是while构造位于子进程中,不会影响其范围之外的变量,而来自fifo或
head -1000 data/output.txt | while read -r ; do something; done