Bash交换数组位置

时间:2018-04-17 01:27:45

标签: bash indexing swap

我需要在字符串数组中交换顺序。

不确定是否可以使用切换索引方法或欢迎任何建议。

示例:

    default=("apple" "banana" "mango" "orange" "peach")

默认输出为:

apple
banana
mango
orange
peach

我需要的输出:

orange <--- switch with apple
banana
mango
apple <--- switch with orange
peach

提前致谢

2 个答案:

答案 0 :(得分:0)

Save Total score is 0.0 Average score is NaN and "${default[0]}" to two separate variables and update the relevant indexes with the values of those variables swapped around.

(Since this looks like an assignment and you haven't included your code I've responded with high level instructions.)

答案 1 :(得分:0)

这会将两个值保存在一个变量中,并将它们分配给数组的另一个元素,如前所述。

firstValue=${default[0]}
fourthValue=${default[3]}
default[3]=$firstValue
default[0]=$fourthValue