复制带有空元素的Bash数组

时间:2022-11-28 15:40:23

I'm having problems in bash (ver 4.2.25) copying arrays with empty elements. When I make a copy of an array into another variable, it does not copy any empty elements along with it.

我在bash(版本4.2.25)中使用空元素复制数组时遇到问题。当我将数组的副本复制到另一个变量时,它不会复制任何空元素。

#!/bin/bash

array=( 'one' '' 'three' )
copy=( ${array[*]} )

IFS=$'\n'

echo "--- array (${#array[*]}) ---"
echo "${array[*]}"

echo
echo "--- copy (${#copy[*]}) ---"
echo "${copy[*]}"

When I do this, here is the output:

当我这样做时,这是输出:

--- array (3) ---
one

three

--- copy (2) ---
one
three

The original array has all three elements including the empty element, but the copy does not. What am I doing wrong here?

原始数组包含所有三个元素,包括空元素,但副本没有。我在这做错了什么?

2 个解决方案

#1


16  

You have a quoting problem and you should be using @, not *. Use:

你有一个引用问题,你应该使用@,而不是*。使用:

copy=( "${array[@]}" )

From the bash(1) man page:

从bash(1)手册页:

Any element of an array may be referenced using ${name[subscript]}. The braces are required to avoid conflicts with pathname expansion. If subscript is @ or *, the word expands to all members of name. These subscripts differ only when the word appears within double quotes. If the word is double-quoted, ${name[*]} expands to a single word with the value of each array member separated by the first character of the IFS special variable, and ${name[@]} expands each element of name to a separate word.

可以使用$ {name [subscript]}引用数组的任何元素。需要大括号以避免与路径名扩展冲突。如果下标是@或*,则该单词将扩展为name的所有成员。这些下标仅在单词出现在双引号内时有所不同。如果单词是双引号,则$ {name [*]}扩展为单个单词,每个数组成员的值由IFS特殊变量的第一个字符分隔,$ {name [@]}扩展每个元素命名为单独的单词。

Example output after that change:

更改后的示例输出:

--- array (3) ---
one

three

--- copy (3) ---
one

three

#2


1  

Starting with Bash 4.3, you can do this

从Bash 4.3开始,您可以执行此操作

$ alpha=(bravo charlie 'delta  3' '' foxtrot)

$ declare -n golf=alpha

$ echo "${golf[2]}"
delta  3

#1


16  

You have a quoting problem and you should be using @, not *. Use:

你有一个引用问题,你应该使用@,而不是*。使用:

copy=( "${array[@]}" )

From the bash(1) man page:

从bash(1)手册页:

Any element of an array may be referenced using ${name[subscript]}. The braces are required to avoid conflicts with pathname expansion. If subscript is @ or *, the word expands to all members of name. These subscripts differ only when the word appears within double quotes. If the word is double-quoted, ${name[*]} expands to a single word with the value of each array member separated by the first character of the IFS special variable, and ${name[@]} expands each element of name to a separate word.

可以使用$ {name [subscript]}引用数组的任何元素。需要大括号以避免与路径名扩展冲突。如果下标是@或*,则该单词将扩展为name的所有成员。这些下标仅在单词出现在双引号内时有所不同。如果单词是双引号,则$ {name [*]}扩展为单个单词,每个数组成员的值由IFS特殊变量的第一个字符分隔,$ {name [@]}扩展每个元素命名为单独的单词。

Example output after that change:

更改后的示例输出:

--- array (3) ---
one

three

--- copy (3) ---
one

three

#2


1  

Starting with Bash 4.3, you can do this

从Bash 4.3开始,您可以执行此操作

$ alpha=(bravo charlie 'delta  3' '' foxtrot)

$ declare -n golf=alpha

$ echo "${golf[2]}"
delta  3