Resetting an array and filling it with values in a bash script
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.
Alex
Updated on June 14, 2022Comments
-
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 thepattern
toprocess_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 thenew_cur
array, and basically make it equal tocurrent
, 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 arraycurrent
doesn't actually change:current=( "${new_cur[@]}" )
Is there a good way to do this? Or a right way to do this?
-
Alex about 9 yearsWill this overwrite all the values at current?
-
pynexj about 9 yearsYes, the old array will be overwritten.
-
Alex about 9 yearsI 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 doingcurrent=( "${cur_new[@]}" )
-
Alex about 9 yearsHow would I load the values from
new_cur
intocurrent
after resetting them? -
pynexj about 9 yearsIt does not work for you? Can you post your new code?
-
Alex about 9 yearsJust 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 outcurrent
doingfor i in "${current[@]}"; do .. printf '%s\n' "$i" .. done
does not work :( -
pynexj about 9 yearsAfter
current=( "${new_cur[@]}" )
please add one linedeclare -p current
to see ifcurrent
has expected values in it. -
Alex about 9 yearsIt outputs
declare -a current='()'
-
pynexj about 9 yearsThere must be something wrong. Try
bash -x /your/script.sh
. -
pynexj about 9 yearsUpdated my answer based on your code. See if it works.
-
Alex about 9 yearsYour 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 about 9 yearsThen you better ask a new question since the array assignment problem has been solved.
-
Alex about 9 yearsWill do! Thank you for all of your help.
-
anubhava about 9 yearsYou already had
for
loop to load values from array1 to array2.