w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
convert an indexed array into an associative array in bash

Here's one way, using sed. Note that this will only work, however, if none of the elements of the original array contain whitespace.

declare -A "newArray=( $(echo ${oldArray[@]} | sed 's/[^
]*/[&]=&/g') )"

The sed command takes each array element 'x' and replaces it with the string '[x]=x', suitable for an associative array assignment.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.