How would you represent EOF in bash?

前端 未结 5 1088
执念已碎
执念已碎 2020-12-24 07:31

I\'m trying to do something like

read -d EOF stdin

for word in $stdin; do stuff; done

where I want to replace \'EOF\' for an actual repre

5条回答
  •  清酒与你
    2020-12-24 08:27

    To find what a control character is, run

    $ cat | od -b
    ^D
    0000000 004 012
    0000002
    

    I typed ^V^D after issuing the command, and then RET and another ^D (unquoted) and the result is that EOF is octal 004.

    Combining that result with read(1):

    $ read -d "$(echo -e '\004')" stdin
    foo
    bar quuz^Hx
    ^D
    $ echo "$stdin"
    foo
    bar quux
    $ for word in $stdin; do echo $word; done
    foo
    bar
    quux
    

    Yes, I typed ^H above for backspace to see if read(1) did the right thing. It does.

提交回复
热议问题