How to copy an array in Bash?

后端 未结 8 1928
陌清茗
陌清茗 2020-12-02 15:31

I have an array of applications, initialized like this:

depends=$(cat ~/Depends.txt)

When I try to parse the list and copy it to a new arra

8条回答
  •  无人及你
    2020-12-02 16:02

    Problem is to copy array in function to be visible in parent code. This solution works for indexed arrays and if before copying are predefined as declare -A ARRAY, works also for associative arrays.

    function array_copy
    # $1 original array name
    # $2 new array name with the same content
    {
        local INDEX
        eval "
            for INDEX in \"\${!$1[@]}\"
            do
                $2[\"\$INDEX\"]=\"\${$1[\$INDEX]}\"
            done
        "
    }
    

提交回复
热议问题