Shell scripting input redirection oddities

前端 未结 9 2061
不思量自难忘°
不思量自难忘° 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条回答
  •  萌比男神i
    2020-12-31 00:36

    This has already been answered correctly, but the solution has not been stated yet. Use ksh, not bash. Compare:

    $ echo 'echo "hello world" | read var1 var2
    echo $var1
    echo $var2' | bash -s
    

    To:

    $ echo 'echo "hello world" | read var1 var2
    echo $var1
    echo $var2' | ksh -s
    hello
    world
    

    ksh is a superior programming shell because of little niceties like this. (bash is the better interactive shell, in my opinion.)

提交回复
热议问题