Timeline for How to pass an array as function argument?
Current License: CC BY-SA 3.0
8 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Feb 10, 2022 at 18:33 | comment | added | Gabriel Staples | You can actually manually pass any number of bash arrays or bash associative arrays, with many arguments, and in any location within the arguments, as I show here and here. Furthermore, bash arrays can even be passed by reference now, which is much easier, as I show here and here. | |
May 28, 2020 at 1:32 | comment | added | David Rissato Cruz |
You can also achieve same behavior without using shift by doing this: local msg="$1"; local -a arr=( "${@:2}" )
|
|
May 8, 2020 at 22:59 | comment | added | Sannu | Explained really well. and didn't know about shift. Thanks. | |
Oct 25, 2019 at 7:07 | comment | added | towi |
You convert "the rest of arguments" into arr . Is it possible to have an array parameter in the middle? Or even several arrays parameters? function copyAndMove() { msg1=$1 ; arr1=...?... ; msg2=? ; arr2=...?... ; msg3=? ; ... } . Like I would define it in python: def copyAndMove(msg1="foo", cpFiles=[], msg2="bar", mvFiles=[], msg3="baz"): ... . Never mind, I found stackoverflow.com/a/4017175/472245
|
|
Jul 4, 2019 at 17:49 | comment | added | SpinUp __ A Davis |
It's also useful to use shift's argument sometimes, so if you had 6 arguments before the array, you can use shift 6 .
|
|
Sep 21, 2018 at 11:03 | comment | added | David 'the bald ginger' | +1 for learning about an array needing to be at the end and that only one should be sent | |
Jan 12, 2018 at 10:27 | review | First posts | |||
Jan 12, 2018 at 11:13 | |||||
Jan 12, 2018 at 10:26 | history | answered | SBF | CC BY-SA 3.0 |