强制bash的扩大变量从文件加载一个字符串 [英] Forcing bash to expand variables in a string loaded from a file

查看:180
本文介绍了强制bash的扩大变量从文件加载一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何让bash的(强制?)在一个字符串变量扩展(这是从文件加载)。

I am trying to work out how to make bash (force?) expand variables in a string (which was loaded from a file).

我有一个名为something.txt的内容的文件:

I have a file called "something.txt" with the contents:

hello $FOO world

我然后运行

export FOO=42
echo $(cat something.txt)

这将返回:

   hello $FOO world

它并没有扩大,即使变量设置$ FOO。我不能EVAL或源文件 - 因为它会尝试并执行它(它不是可执行的,因为它是 - 我只是想插入变量的字符串)。

It didn't expand $FOO even though the variable was set. I can't eval or source the file - as it will try and execute it (it isn't executable as it is - I just want the string with the variables interpolated).

任何想法?

推荐答案

我无意中发现了什么,我觉得是这个问题的答案:在 envsubst 命令

I stumbled on what I think is THE answer to this question: the envsubst command.

envsubst < something.txt

在情况下,它是不是已经在你的发行版可用,它在
GNU包的gettext

In case it's not already available in your distro, it's in the GNU package gettext.

这篇关于强制bash的扩大变量从文件加载一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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