bash 如何在bash中逐列组合两个变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18437124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to combine two variable column-by-column in bash
提问by valpa
I have two variables, multi-line.
我有两个变量,多行。
VAR1="1
2
3
4"
VAR2="ao
ad
af
ae"
I want to get
我想得到
VAR3="1ao
2ad
3af
4ae"
I know I can do it by:
我知道我可以通过以下方式做到:
echo "$VAR1" > /tmp/order
echo "$VAR2" | paste /tmp/order -
But is there any way to do without a temp file?
但是没有临时文件有什么办法吗?
回答by ДМИТРИЙ МАЛИКОВ
paste <(echo "$VAR1") <(echo "$VAR2") --delimiters ''
paste <(echo "$VAR1") <(echo "$VAR2") --delimiters ''
回答by devnull
You can say:
你可以说:
$ VAR3=$(paste <(echo "$VAR1") <(echo "$VAR2"))
$ echo "$VAR3"
1 ao
2 ad
3 af
4 ae
It's not clear whether you want spaces in the resulting array or not. Your example that workswould contain spaces as in the above case.
目前尚不清楚您是否想要结果数组中的空格。你的榜样,工程会包含空格,如上述情况。
If you don't want spaces, i.e. 1ao
instead of 1 ao
, then you can say:
如果你不想要空格,即1ao
代替1 ao
,那么你可以说:
$ VAR3=$(paste <(echo "$VAR1") <(echo "$VAR2") -d '')
$ echo "$VAR3"
1ao
2ad
3af
4ae