Shell scripting input redirection oddities

前端 未结 9 2072
不思量自难忘°
不思量自难忘° 2020-12-31 00:00

Can anyone explain this behavior? Running:

#!/bin/sh
echo \"hello world\" | read var1 var2
echo $var1
echo $var2

results in nothing being o

9条回答
  •  爱一瞬间的悲伤
    2020-12-31 00:50

    A recent addition to bash is the lastpipe option, which allows the last command in a pipeline to run in the current shell, not a subshell, when job control is deactivated.

    #!/bin/bash
    set +m      # Deactiveate job control
    shopt -s lastpipe
    echo "hello world" | read var1 var2
    echo $var1
    echo $var2
    

    will indeed output

    hello
    world
    

提交回复
热议问题