如何 cat 多行 bash 变量?

ani*_*ita 1 unix variables bash cat

当我尝试保存 bash 变量列,然后尝试将它们组合在一起时,出现错误。

这些是变量:

$ a_var=$(grep -i "word" {input} | awk '{print $3}')

$ echo $a_var
hello1
hello2
hello3

$ b_var=$(grep -i "otherword" {input} | awk '{print $3}')

$ echo $b_var
hello10
hello11
hello12
Run Code Online (Sandbox Code Playgroud)

但是当我尝试时:

$ new_var=$(cat $a $b)
Run Code Online (Sandbox Code Playgroud)

我得到一些无法保存到文件或新变量的奇怪输出。

cat: hello1: No such file or directory
cat: hello2: No such file or directory
cat: hello3: No such file or directory
cat: hello10: No such file or directory
cat: hello11: No such file or directory
cat: hello12: No such file or directory

$ echo $new_var

$
Run Code Online (Sandbox Code Playgroud)

(当我“回声”时,返回空白。)

到底是怎么回事?如何将我的 bash 变量连接在一起?

编辑:我希望输出看起来像这样(我想将此列值保存到文件中):

hello1
hello2
hello3
hello10
hello11
hello12
Run Code Online (Sandbox Code Playgroud)

小智 7

尽管cat在 Linux 中代表 concatenate,但它不会作用于变量。实际上,它用于连接文件并打印到标准输出设备。对于您的情况,请使用c_var="${a_var}${b_var}"