Timeline for What is the most correct way to pass an array to a function?
Current License: CC BY-SA 3.0
7 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Apr 13, 2017 at 12:36 | history | edited | CommunityBot |
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
|
|
| Jun 26, 2012 at 15:11 | vote | accept | rahmu | ||
| Jun 23, 2012 at 22:56 | comment | added | Gilles 'SO- stop being evil' |
@rahmu There are two parameters: fear and surprise… and ruthless efficiency. (In other words, you're right, there's a typo: hello, world and wibble make 3 parameters.)
|
|
| Jun 23, 2012 at 22:40 | comment | added | rahmu | Correct me if I'm wrong, but I believe there's a typo in what you wrote: the unquoted expansion passes 3 params, not 2. | |
| Jun 23, 2012 at 21:47 | comment | added | Gilles 'SO- stop being evil' |
@rahmu The quotes are needed to avoid splitting and globbing on individual elements. Consider myarray=("hello world" wibble) (2 elements, the first of which contains a space): work_on_array "${myarray[@]}" passes 2 parameters hello world and wibble; work_on_array ${myarray[@]} passes 2 parameters hello, world and wibble. And with myarray=(*), work_on_array ${myarray[@]} passes the list of files in the current directory. (Hence this is one of the many cases where my advice makes a practical difference.)
|
|
| Jun 23, 2012 at 10:16 | comment | added | rahmu | Thanks. Question: if the result of the expansion is not one word, why are the quotes needed? Can they be omitted? Are you just applying your advice of "always quote unless you have a good reason not to"? :p | |
| Jun 23, 2012 at 0:57 | history | answered | Gilles 'SO- stop being evil' | CC BY-SA 3.0 |