简体   繁体   English

如何 cat 多行 bash 变量?

[英]How to cat multi-line bash variables?

When I try to save columns of bash variables and then try to cat them together, I get an error.当我尝试保存 bash 变量列,然后尝试将它们组合在一起时,出现错误。

These are the variables:这些是变量:

$ 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

But then when I try:但是当我尝试时:

$ new_var=$(cat $a $b)

I get some weird output that can't save to a file or a new variable.我得到一些无法保存到文件或新变量的奇怪输出。

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

$

(When I "echo" there is a blank return.) (当我“回声”时,返回空白。)

What is going on?到底是怎么回事? How can I concatenate my bash variables together?如何将我的 bash 变量连接在一起?

Edit: I want the output to look like this (I want to save this column of values to a file):编辑:我希望输出看起来像这样(我想将此列值保存到文件中):

hello1
hello2
hello3
hello10
hello11
hello12

Although cat in Linux stands for concatenate, it not going to act on variables.尽管 Linux 中的cat代表 concatenate,但它不会作用于变量。 Actually, it is used to connect files and print to the standard output device.实际上,它用于连接文件并打印到标准输出设备。 For your case, use c_var="${a_var}${b_var}"对于您的情况,请使用c_var="${a_var}${b_var}"

This is how you concatenate these string variables in shell:这是在 shell 中连接这些字符串变量的方式:

c_var="$a_var$b_var" ; c_var="$a_var$b_var" ; echo $c_var回声 $c_var

Or generally: c="$a$b" ;或者一般: c="$a$b" ; echo $c回声 $c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM