Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

syntax - Why source command doesn't work with process substitution in bash 3.2?

I've the following shell script:

cat <(echo foo)
source <(echo bar=bar)
echo $bar

However it works differently in GNU bash 3.2 and 4.3 as shown below:

$ /bin/bash foo.sh 
foo

3.2.53(1)-release

$ /usr/local/bin/bash foo.sh 
foo
bar
4.3.33(1)-release

Why this works only on one version? Is it a bug or added feature?

It seems the process substitution works fine, however problem lay when sourcing the file.

If this is expected behaviour, what other syntax should I use instead to source something from the standard input to be compatible between different bash versions?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a known limitation in bash 3.2. To work around it:

source /dev/stdin <<<"$(echo bar=bar)"

...or, similarly:

source /dev/stdin <<<"$(cat <(...))"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...