Resetting an array and filling it with values in a bash script

10,862

Solution 1

You can simply clone an array using array1=( "${array2[@]}" ). For example:

[STEP 100] $ echo $BASH_VERSION
4.3.33(1)-release
[STEP 101] $ cat foo.sh
current=(aa bb cc)

process_name ()
{
    local new_cur=()

    for i in "${current[@]}"; do
        if [[ "$i" == "$1" ]]; then
            new_cur+=("$i")
        fi
    done
    current=( "${new_cur[@]}" )
}

process_name aa

for i in "${current[@]}"; do
    printf '%s\n' "$i"
done
[STEP 102] $ bash foo.sh
aa
[STEP 103] $

Solution 2

To reset an array just use:

current=()

This will delete old entries and declare a 0 element array.

Share:
10,862
Alex
Author by

Alex

Updated on June 14, 2022

Comments

  • Alex
    Alex almost 2 years

    I wanted to know the "right" way to do this. Basically, I have a list of files that are in an array called current. This is declared as a global variable that looks like this: current=(). I have successfully put all the files in this array. But now, I am going through and parsing arguments to filter out these files and directories.

    For example, to implement the -name '*.pattern' command, I pass in the pattern to process_name() which does this:

    process_name ()
    {
        local new_cur=()
        for i in "${current[@]}"; do
            if [[ "$i" == "$1" ]]; then
                new_cur+=("$i")
            fi
        done
        current=( "${new_cur[@]}" )
    }
    

    After the loop finishes I want to "clear" my current array. Then I want to loop over the new_cur array, and basically make it equal to current, or if I can, just do something like $current = $new_cur (although I know this won't work).

    I've tried doing this after the for loop (in process_name()), but my array current doesn't actually change:

    current=( "${new_cur[@]}" )
    

    Is there a good way to do this? Or a right way to do this?

  • Alex
    Alex about 9 years
    Will this overwrite all the values at current?
  • pynexj
    pynexj about 9 years
    Yes, the old array will be overwritten.
  • Alex
    Alex about 9 years
    I did some testing and even though my local variable has the correct values... after resetting the array with the solution you provided, current ends up empty, basically, cur_new never gets written into it. I'm doing current=( "${cur_new[@]}" )
  • Alex
    Alex about 9 years
    How would I load the values from new_cur into current after resetting them?
  • pynexj
    pynexj about 9 years
    It does not work for you? Can you post your new code?
  • Alex
    Alex about 9 years
    Just updated it.. I realized I didn't need to check for the types yet. And printing out new_cur in the loop yields correct results. However, once out of the function, printing out current doing for i in "${current[@]}"; do .. printf '%s\n' "$i" .. done does not work :(
  • pynexj
    pynexj about 9 years
    After current=( "${new_cur[@]}" ) please add one line declare -p current to see if current has expected values in it.
  • Alex
    Alex about 9 years
    It outputs declare -a current='()'
  • pynexj
    pynexj about 9 years
    There must be something wrong. Try bash -x /your/script.sh.
  • pynexj
    pynexj about 9 years
    Updated my answer based on your code. See if it works.
  • Alex
    Alex about 9 years
    Your script does work on it's own, but attempts to implement it in mine are failing :( I have my script here, with the usage: pastebin.com/YmVjPMBj Thank you for all of the help!
  • pynexj
    pynexj about 9 years
    Then you better ask a new question since the array assignment problem has been solved.
  • Alex
    Alex about 9 years
    Will do! Thank you for all of your help.
  • anubhava
    anubhava about 9 years
    You already had for loop to load values from array1 to array2.