How to pass an array as function argument?

Ahsanul Haque

Struggling for a while passing an array as argument but it's not working anyway. I've tried like below:

#! /bin/bash

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

}

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

copyFiles $array

An answer with explanation would be nice.

Edit: Basically, i will eventually call the function from another script file. Plz explain the constraints if possible.

A.B.
  • 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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pass an array argument to a function

From Dev

How to properly pass an array as a function argument?

From Dev

How to pass in a dynamically allocated array as an argument to a function

From Dev

How to pass argument to "then" function

From Dev

How to pass a function as an argument

From Dev

Pass in part of an array as function argument

From Dev

Pass numpy array as function argument

From Dev

How to produce a character array in ctypes and pass its pointer as a function argument

From Dev

How to pass an array as argument (in Python) when solving ODE by odeint function

From Dev

How to pass a list, vector or array as argument in invokeMethod function

From

How to pass a 2 dimensional array as a function argument in Go?

From

How to pass multidimensional array in arbitrary size as function argument in GO

From Dev

How to pass array as an argument to a Ruby function from command line?

From Dev

how to pass a swift Array as UnsafePointer<T> argument in a function

From Dev

How to pass whole numpy array as argument in python to a user defined function?

From Dev

How to pass a 2 Dimensional array as an argument of function in C?

From Dev

How to pass a constant array as argument to a C++ function/method

From Dev

How to pass the value of an array as a second argument to awk's split function?

From Dev

How to pass a pointer to a function argument of type fixed size array?

From Dev

How to pass json array as function argument in postgresql 9.6

From Java

How to pass layout as a function argument

From Dev

How to pass a function as an argument, with arguments?

From

How to pass a struct as an argument of a function

From Dev

How to pass extension function as argument?

From

how to pass dynamic argument in function

From Dev

How to pass a function as argument in Rust

From Dev

How to pass a list as an argument to a function

From Dev

How to pass a function or expression as an argument?

From Dev

How to pass QTableWidget as an argument on function

Related Related

  1. 1

    How to pass an array argument to a function

  2. 2

    How to properly pass an array as a function argument?

  3. 3

    How to pass in a dynamically allocated array as an argument to a function

  4. 4

    How to pass argument to "then" function

  5. 5

    How to pass a function as an argument

  6. 6

    Pass in part of an array as function argument

  7. 7

    Pass numpy array as function argument

  8. 8

    How to produce a character array in ctypes and pass its pointer as a function argument

  9. 9

    How to pass an array as argument (in Python) when solving ODE by odeint function

  10. 10

    How to pass a list, vector or array as argument in invokeMethod function

  11. 11

    How to pass a 2 dimensional array as a function argument in Go?

  12. 12

    How to pass multidimensional array in arbitrary size as function argument in GO

  13. 13

    How to pass array as an argument to a Ruby function from command line?

  14. 14

    how to pass a swift Array as UnsafePointer<T> argument in a function

  15. 15

    How to pass whole numpy array as argument in python to a user defined function?

  16. 16

    How to pass a 2 Dimensional array as an argument of function in C?

  17. 17

    How to pass a constant array as argument to a C++ function/method

  18. 18

    How to pass the value of an array as a second argument to awk's split function?

  19. 19

    How to pass a pointer to a function argument of type fixed size array?

  20. 20

    How to pass json array as function argument in postgresql 9.6

  21. 21

    How to pass layout as a function argument

  22. 22

    How to pass a function as an argument, with arguments?

  23. 23

    How to pass a struct as an argument of a function

  24. 24

    How to pass extension function as argument?

  25. 25

    how to pass dynamic argument in function

  26. 26

    How to pass a function as argument in Rust

  27. 27

    How to pass a list as an argument to a function

  28. 28

    How to pass a function or expression as an argument?

  29. 29

    How to pass QTableWidget as an argument on function

HotTag

Archive