Skip to main content
added 5 characters in body
Source Link
Manuel Jordan
  • 2.2k
  • 4
  • 26
  • 61

The following post solution works as expected:

Therefore - from his answer:

function copyFiles() {
   arr=("$@")
   for i in "${arr[@]}";
      do
          echo "$i"
      done

}

array=("one 1" "two 2" "three 3")

copyFiles "${array[@]}"

The reason of this post is what if the following case happens:

copyFiles "${array[@]}" "Something More"
copyFiles "Something More" "${array[@]}"

Problem: I did do realize about the parametersarguments sent, when they are received how argumentsparameters in the function - they are practically merged - so $1 and $2 does not work as expected any more, the "array" argument integrates with the other argument

I already did do a research:

Sadly typeset -n does not work

and in:

does not work as expected - in that answer there is a comment indicating an issue - with a link with a demo testing/verification - about the array size (${#array[@]}) being different within the function.

So how accomplish this goal?

The following post solution works as expected:

Therefore - from his answer:

function copyFiles() {
   arr=("$@")
   for i in "${arr[@]}";
      do
          echo "$i"
      done

}

array=("one 1" "two 2" "three 3")

copyFiles "${array[@]}"

The reason of this post is what if the following case happens:

copyFiles "${array[@]}" "Something More"
copyFiles "Something More" "${array[@]}"

Problem: I did do realize about the parameters sent, when they are received how arguments in the function - they are practically merged - so $1 and $2 does not work as expected any more, the "array" argument integrates the other argument

I already did do a research:

Sadly typeset -n does not work

and in:

does not work as expected - in that answer there is a comment indicating an issue - with a link with a demo testing/verification - about the array size (${#array[@]}) being different within the function.

So how accomplish this goal?

The following post solution works as expected:

Therefore - from his answer:

function copyFiles() {
   arr=("$@")
   for i in "${arr[@]}";
      do
          echo "$i"
      done

}

array=("one 1" "two 2" "three 3")

copyFiles "${array[@]}"

The reason of this post is what if the following case happens:

copyFiles "${array[@]}" "Something More"
copyFiles "Something More" "${array[@]}"

Problem: I did do realize about the arguments sent, when they are received how parameters in the function - they are practically merged - so $1 and $2 does not work as expected any more, the "array" argument integrates with the other argument

I already did do a research:

Sadly typeset -n does not work

and in:

does not work as expected - in that answer there is a comment indicating an issue - with a link with a demo testing/verification - about the array size (${#array[@]}) being different within the function.

So how accomplish this goal?

Source Link
Manuel Jordan
  • 2.2k
  • 4
  • 26
  • 61

How to pass an array as function argument but with other extra parameters?

The following post solution works as expected:

Therefore - from his answer:

function copyFiles() {
   arr=("$@")
   for i in "${arr[@]}";
      do
          echo "$i"
      done

}

array=("one 1" "two 2" "three 3")

copyFiles "${array[@]}"

The reason of this post is what if the following case happens:

copyFiles "${array[@]}" "Something More"
copyFiles "Something More" "${array[@]}"

Problem: I did do realize about the parameters sent, when they are received how arguments in the function - they are practically merged - so $1 and $2 does not work as expected any more, the "array" argument integrates the other argument

I already did do a research:

Sadly typeset -n does not work

and in:

does not work as expected - in that answer there is a comment indicating an issue - with a link with a demo testing/verification - about the array size (${#array[@]}) being different within the function.

So how accomplish this goal?