【问题标题】:variables are not interpreted when I use the "cat" command to put the contents of a text file into a variable当我使用“cat”命令将文本文件的内容放入变量时,变量不会被解释
【发布时间】:2021-12-08 01:19:13
【问题描述】:

我需要一些帮助,我有一个脚本 (script.sh),内容如下:

#!/bin/bash

export name='toto'
file='file.txt'
content=$(cat "file.txt")
echo "$content"

file.txt 内容一些文本: 我是$name

现在当我运行我的脚本 (script.sh) 时,变量 $name 未被解释,我得到如下结果: 我是 $name 我想要这样的结果:I am toto

【问题讨论】:

标签: bash variables cat


【解决方案1】:

您可以为此使用envsubst

content=$(envsubst < "file.txt")

【讨论】:

  • 如果没有UUOC,这个好答案会更好。
猜你喜欢
  • 2014-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多