Created
December 11, 2012 05:18
-
-
Save yegle/4256080 to your computer and use it in GitHub Desktop.
bash read varaible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# FAIL! | |
echo a| read i | |
echo $i | |
# WORKS | |
read i <<<$(echo a) | |
echo $i |
Bash 4.2 有 lastpipe
选项,开启了就和 ksh 一样了,管道的最后一部分在当前 shell 运行。
shopt -s lastpipe
不小心看到了,进来发发牢骚。另外,竟然不能修改评论,你能修改的话,把上一条的格式调整下吧。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bash 的管道都是在 subshell 中执行的,所以第一个 loop 中的变量不会影响到当前 shell 环境,可以使用语句块,都放到 subshell 去。
while read i
do
echo $i
done < <(echo a b c)