Linux bash:多变量赋值

发布于 2024-08-16 06:41:41 字数 703 浏览 4 评论 0原文

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

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

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

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

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

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

不过,有效的方法如下:

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

但是索引数组并不像普通变量名称那样具有描述性。
然而,我可以这样做:

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

但我更愿意避免另外 3 个陈述。

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

Does exist in linux bash something similar to the following code in 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.

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

不即不离 2024-08-23 06:41:42

我首先想到的是:

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

输出是,毫不奇怪

1|2|3

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
围归者 2024-08-23 06:41:42

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

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

其产量:

2|4|6 

正如预期的那样。

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

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

which yields:

2|4|6 

as expected.

乖乖 2024-08-23 06:41:42

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

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

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

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:

DATE_ARRAY=(`echo $2 | sed -e 's/\// /g'`)
MONTH=(`echo ${DATE_ARRAY[0]}`)
DAY=(`echo ${DATE_ARRAY[1]}`)
YEAR=(`echo ${DATE_ARRAY[2]}`)
随梦而飞# 2024-08-23 06:41:42

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

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

您也可以通过管道输入 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

结果...

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

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.

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:

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
甜警司 2024-08-23 06:41:42

如果要控制分隔字符(即单个字符),可以将IFSread -r结合使用。 <<<< '<列表>'

IFS=',' read -r foo bar baz <<< 'foo,bar,baz'
echo "foo=$foo bar=$bar baz=$baz"
# => foo=foo bar=bar baz=baz

If you want to control the delimiting character (i.e. single character), you can use IFS in conjunction with read -r <vars> <<< '<list>':

IFS=',' read -r foo bar baz <<< 'foo,bar,baz'
echo "foo=$foo bar=$bar baz=$baz"
# => foo=foo bar=bar baz=baz
神爱温柔 2024-08-23 06:41:42

O'Reilly 的 Bash Cookbook 的第 5 章(详细地)讨论了该要求的原因变量赋值,“=”符号周围没有空格。

MYVAR="something"

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

这一切看起来有点像事后证明,但无论如何都没有提到分配给变量列表的方法。

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

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.

痕至 2024-08-23 06:41:42

让 var1=var2=var3=0

var1=var2=var3=“默认值”

let var1=var2=var3=0
or
var1=var2=var3="Default value"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文