Linux bash:多变量赋值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1952404/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:37:47  来源:igfitidea点击:

Linux bash: Multiple variable assignment

linuxbashshellvariable-assignmentmultiple-variable-return

提问by GetFree

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

linux bash 中是否存在类似于以下 PHP 代码的内容:

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

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

即您在一个句子中为 3 个不同的变量分配了一个相应的值。

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

假设我有将myBashFuntion字符串“qwert asdfg zxcvb”写入标准输出的 bash 函数。是否可以执行以下操作:

(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]}

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

但这些是我更愿意避免的另外 3 个陈述。

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

我只是在寻找快捷语法。是否可以?

采纳答案by Michael Krelin - hacker

First thing that comes into my mind:

我想到的第一件事:

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

output is, unsurprisingly

不出所料,输出是

1|2|3

回答by pavium

Chapter 5 of the Bash Cookbookby O'Reilly, discusses (at some length) the reasons for the requirement in a variable assignment that there be no spaces around the '=' sign

O'Reilly的Bash Cookbook的第 5 章(以一定的篇幅)讨论了在变量赋值中要求 '=' 符号周围没有空格的原因

MYVAR="something"

The explanation has something to do with distinguishing between the name of a command and a variable (where '=' may be a valid argument).

该解释与区分命令名和变量名有关(其中 '=' 可能是有效参数)。

This all seems a little like justifying after the event, but in any case there is no mention of a method of assigning to a list of variables.

这一切似乎有点像在事件之后证明是合理的,但无论如何都没有提到分配给变量列表的方法。

回答by SDGuero

I think this might help...

我认为这可能会有所帮助...

In order to break down user inputted dates (mm/dd/yyyy) in my scripts, I store the day, month, and year into an array, and then put the values into separate variables as follows:

为了在我的脚本中分解用户输入的日期 (mm/dd/yyyy),我将日、月和年存储到一个数组中,然后将这些值放入单独的变量中,如下所示:

DATE_ARRAY=(`echo  | sed -e 's/\// /g'`)
MONTH=(`echo ${DATE_ARRAY[0]}`)
DAY=(`echo ${DATE_ARRAY[1]}`)
YEAR=(`echo ${DATE_ARRAY[2]}`)

回答by Otheus

Sometimes you have to do something funky. Let's say you want to read from a command (the date example by SDGuero for example) but you want to avoid multiple forks.

有时你必须做一些时髦的事情。假设您想从命令中读取(例如 SDGuero 的日期示例),但您想避免多次分叉。

read month day year << DATE_COMMAND
 $(date "+%m %d %Y")
DATE_COMMAND
echo $month $day $year

You could also pipe into the read command, but then you'd have to use the variables within a subshell:

您也可以通过管道输入 read 命令,但是您必须在子shell中使用变量:

day=n/a; month=n/a; year=n/a
date "+%d %m %Y" | { read day month year ; echo $day $month $year; }
echo $day $month $year

results in...

结果是...

13 08 2013
n/a n/a n/a

回答by soundray

I wanted to assign the values to an array. So, extending Michael Krelin's approach, I did:

我想将值分配给一个数组。因此,扩展Michael Krelin 的方法,我做到了:

read a[{1..3}] <<< $(echo 2 4 6); echo "${a[1]}|${a[2]}|${a[3]}"

which yields:

产生:

2|4|6 

as expected.

正如预期的那样。