Skip to main content
Add spaces to the values passed.
Source Link
kbenoit
  • 2.4k
  • 1
  • 13
  • 17
  • Expanding an array without an index only gives the first element, use

      copyFiles "${array[@]}"
    

instead of

    copyFiles $array
  • Use a she-bang

      #!/bin/bash
    
  • Use the correct function syntax

Valid variants are

    function copyFiles {…}
    function copyFiles(){…}
    function copyFiles() {…}

instead of

    function copyFiles{…}
  • Use the right syntax to get the array parameter

      arr=("$@")
    

instead of

    arr="$1"

Therefore

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

}

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

copyFiles "${array[@]}"

Output is (my script has the name foo)

$ ./foo   
one 1
two 2
three 3
  • Expanding an array without an index only gives the first element, use

      copyFiles "${array[@]}"
    

instead of

    copyFiles $array
  • Use a she-bang

      #!/bin/bash
    
  • Use the correct function syntax

Valid variants are

    function copyFiles {…}
    function copyFiles(){…}
    function copyFiles() {…}

instead of

    function copyFiles{…}
  • Use the right syntax to get the array parameter

      arr=("$@")
    

instead of

    arr="$1"

Therefore

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

}

array=("one" "two" "three")

copyFiles "${array[@]}"

Output is (my script has the name foo)

$ ./foo   
one
two
three
  • Expanding an array without an index only gives the first element, use

      copyFiles "${array[@]}"
    

instead of

    copyFiles $array
  • Use a she-bang

      #!/bin/bash
    
  • Use the correct function syntax

Valid variants are

    function copyFiles {…}
    function copyFiles(){…}
    function copyFiles() {…}

instead of

    function copyFiles{…}
  • Use the right syntax to get the array parameter

      arr=("$@")
    

instead of

    arr="$1"

Therefore

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

}

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

copyFiles "${array[@]}"

Output is (my script has the name foo)

$ ./foo   
one 1
two 2
three 3
added 88 characters in body
Source Link
A.B.
  • 90.9k
  • 22
  • 249
  • 325
  • Expanding an array without an index only gives the first element, use

      copyFiles "${array[@]}"
    

instead of

    copyFiles $array
  • Use a she-bang

      #!/bin/bash
    
  • Use the correct function syntax

      function copyFiles() {…}
    

Valid variants are

    function copyFiles {…}
    function copyFiles(){…}
    function copyFiles() {…}

instead of

    function copyFiles{…}
  • Use the right syntax to get the array parameter

      arr=("$@")
    

instead of

    arr="$1"

Therefore

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

}

array=("one" "two" "three")

copyFiles "${array[@]}"

Output is (my script has the name foo)

$ ./foo   
one
two
three
  • Expanding an array without an index only gives the first element, use

      copyFiles "${array[@]}"
    

instead of

    copyFiles $array
  • Use a she-bang

      #!/bin/bash
    
  • Use the correct function syntax

      function copyFiles() {…}
    

instead of

    function copyFiles{…}
  • Use the right syntax to get the array parameter

      arr=("$@")
    

instead of

    arr="$1"

Therefore

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

}

array=("one" "two" "three")

copyFiles "${array[@]}"

Output is (my script has the name foo)

$ ./foo   
one
two
three
  • Expanding an array without an index only gives the first element, use

      copyFiles "${array[@]}"
    

instead of

    copyFiles $array
  • Use a she-bang

      #!/bin/bash
    
  • Use the correct function syntax

Valid variants are

    function copyFiles {…}
    function copyFiles(){…}
    function copyFiles() {…}

instead of

    function copyFiles{…}
  • Use the right syntax to get the array parameter

      arr=("$@")
    

instead of

    arr="$1"

Therefore

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

}

array=("one" "two" "three")

copyFiles "${array[@]}"

Output is (my script has the name foo)

$ ./foo   
one
two
three
added 205 characters in body
Source Link
A.B.
  • 90.9k
  • 22
  • 249
  • 325
  • Expanding an array without an index only gives the first element, use

      copyFiles "${array[@]}"
    

instead of

    copyFiles $array
  • Use a she-bang

      #!/bin/bash
    
  • Use the correct function syntax

      function copyFiles() {…}
    

instead of

    function copyFiles{…}
  • Use the right syntax to get the array parameter

      arr=("$@")
    

instead of

    arr="$1"

Therefore

#!/bin/bash
function copyFiles() {
    a=arr=("$@")
    for i in "${a[@]arr[@]}"";
 ;     do
          echo "$i"
      done 

}

array=("one" "two" "three")

copyFiles "${array[@]}"

Output is (my script has the name foo)

$ ./foo   
one
two
three
  • Expanding an array without an index only gives the first element, use

      copyFiles "${array[@]}"
    

instead of

    copyFiles $array
  • Use a she-bang

      #!/bin/bash
    
  • Use the correct function syntax

      function copyFiles() {…}
    

Therefore

#!/bin/bash
function copyFiles() {
    a=("$@")
    for i in "${a[@]}" ; do
        echo "$i"
    done
}

array=("one" "two" "three")

copyFiles "${array[@]}"
  • Expanding an array without an index only gives the first element, use

      copyFiles "${array[@]}"
    

instead of

    copyFiles $array
  • Use a she-bang

      #!/bin/bash
    
  • Use the correct function syntax

      function copyFiles() {…}
    

instead of

    function copyFiles{…}
  • Use the right syntax to get the array parameter

      arr=("$@")
    

instead of

    arr="$1"

Therefore

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

}

array=("one" "two" "three")

copyFiles "${array[@]}"

Output is (my script has the name foo)

$ ./foo   
one
two
three
Source Link
A.B.
  • 90.9k
  • 22
  • 249
  • 325
Loading