【发布时间】:2021-10-02 22:50:06
【问题描述】:
当我尝试保存 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
但是当我尝试时:
$ new_var=$(cat $a $b)
我得到一些无法保存到文件或新变量的奇怪输出。
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
$
(当我“回显”时,返回空白。)
发生了什么事?如何将我的 bash 变量连接在一起?
编辑:我希望输出看起来像这样(我想将此列值保存到文件中):
hello1
hello2
hello3
hello10
hello11
hello12
【问题讨论】:
-
使用
printf '%s\n' "$a" "$b" -
也许:
new_var="$(cat $a $b)" -
@anita :你为什么提到数组?您的示例中没有数组。
-
你想要哪个输出?
-
@anubhava 的回答为我做了。谢谢!