Linux的庆典:多变量赋值 [英] Linux bash: Multiple variable assignment

查看:120
本文介绍了Linux的庆典:多变量赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在linux bash中确实存在类似PHP以下code的内容:

Does exist in linux bash something similar to the following code in PHP:

list($var1, $var2, $var3) = function_that_returns_a_three_element_array() ;

即。你一句话相应的值分配给3个不同的变量。

i.e. you assign in one sentence a corresponding value to 3 different variables.

让我们说我有bash的功能 myBashFuntion 写入到标准输出字符串QWERT asdfg zxcvb。
是否有可能做这样的事情:

Let's say I have the bash function myBashFuntion that writes to stdout the string "qwert asdfg zxcvb". Is it possible to do something like:

(var1 var2 var3) = ( `myBashFuntion param1 param2` )

在等号左边的部分当然不是有效的语法。我只是想解释一下我所要求的。

The part at the left of the equal sign is not valid syntax of course. I'm just trying to explain what I'm asking for.

这是什么工作,不过,如下:

What does work, though, is the following:

array = ( `myBashFuntion param1 param2` )
echo ${array[0]} ${array[1]} ${array[2]}

但是,一个索引数组不为纯变量名称为描述性的。结果
不过,我可能只是这样做:

But an indexed array is not as descriptive as plain variable names.
However, I could just do:

var1 = ${array[0]} ; var2 = ${array[1]} ; var3 = ${array[2]}

但这些都是3,我会preFER避免更多的语句。

But those are 3 more statements that I'd prefer to avoid.

我只是在寻找一个快捷方式的语法。这可能吗?

I'm just looking for a shortcut syntax. Is it possible?

推荐答案

第一件事进入我的脑海:

First thing that comes into my mind:

read a b c <<<$(echo 1 2 3) ; echo "$a|$b|$c"

输出是勿庸置疑

1|2|3

这篇关于Linux的庆典:多变量赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆