为什么 source 命令不能与 bash 3.2 中的进程替换一起使用? [英] Why source command doesn't work with process substitution in bash 3.2?

查看:13
本文介绍了为什么 source 命令不能与 bash 3.2 中的进程替换一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 shell 脚本:

I've the following shell script:

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

但是它在 GNU bash 3.2 和 4.3 中的工作方式不同,如下所示:

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.

如果这是预期的行为,我应该使用什么其他语法来source 标准输入中的某些内容以在不同的 bash 版本之间兼容?

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?

推荐答案

这是一个 bash 3.2 中的已知限制.解决这个问题:

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

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

...或者,类似地:

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

这篇关于为什么 source 命令不能与 bash 3.2 中的进程替换一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆