将文本文件复制到Bash中的变量[重复]

时间:2021-01-29 15:45:28

This question already has an answer here:

这个问题在这里已有答案:

Is there a way to copy the content of a text file to a variable in Bash?

有没有办法将文本文件的内容复制到Bash中的变量?

Let's say I have a file containing some text, and I would like to modify text but not a file itself. How can I copy the content of this file to a variable, and then modify the variable?

假设我有一个包含一些文本的文件,我想修改文本而不是文件本身。如何将此文件的内容复制到变量,然后修改变量?

1 个解决方案

#1


0  

I'm not very clear on what you're asking, but I think this is what you're after.

我不太清楚你在问什么,但我认为这就是你所追求的。

if you have file.txt, you can do this

如果你有file.txt,你可以这样做

var1=$(cat /path/to/file.txt)

var1 = $(cat /path/to/file.txt)

you can then manipulate it how you please.

然后你可以随心所欲地操纵它。

edit:

编辑:

You can access the variable by $var1, i.e. echo "$var1"

您可以通过$ var1访问变量,即echo“$ var1”

#1


0  

I'm not very clear on what you're asking, but I think this is what you're after.

我不太清楚你在问什么,但我认为这就是你所追求的。

if you have file.txt, you can do this

如果你有file.txt,你可以这样做

var1=$(cat /path/to/file.txt)

var1 = $(cat /path/to/file.txt)

you can then manipulate it how you please.

然后你可以随心所欲地操纵它。

edit:

编辑:

You can access the variable by $var1, i.e. echo "$var1"

您可以通过$ var1访问变量,即echo“$ var1”