Skip to main content
Corrected spelling, fixed grammar
Source Link
karel
  • 116.9k
  • 116
  • 283
  • 315

The best way is to pass as position arguments. Nothing else. You may pass as string. But, but this way may cause some troubles. Example:

array=(one two three forfour five)

function show_passed_array(){
  echo $@
}

or

function show_passed_array(){
  while $# -gt 0;do
    echo $1;shift
  done
}

    show_passed_array ${array[@]}

output:

  one two three forfour five

You mean if array value has space symbols you must quoitquote elements first before pass for assesaccessing value by index in function use $1 $2 $3 ... position parameters. Where index 0 -> 1, 1 -> 2,... To iterate access it is best to use always $1 and after shiftShift. Nothing additional is needed. You may pass arguments without any array like thatthis:

show_pased_arrayshow_passed_array one two three forfour five

bash media automatically buildbuilds an array from passed arguments that passed them to function and then you have position arguments. ForemoreFurthermore when you write ${array[2]} you in real whitereally write consequent argument one one two three four and passed them to the function. So those calls are equivalent.

best way to pass as position arguments. Nothing else. You may pass as string. But this way may some troubles. Example

array=(one two three for five)

function show_passed_array(){
  echo $@
}

or

function show_passed_array(){
  while $# -gt 0;do
    echo $1;shift
  done
}

    show_passed_array ${array[@]}

output:

  one two three for five

You mean if array value has space symbols you must quoit elements first before pass for asses value by index in function use $1 $2 $3 ... position parameters. Where index 0 -> 1, 1 -> 2,... To iterate access best use always $1 and after shift. Nothing additional needed. You may pass arguments without any array like that:

show_pased_array one two three for five

bash media automatically build array from passed arguments that passed them to function and then you have position arguments. Foremore when you write ${array[2]} you in real white consequent argument one two three four and passed them to function. So those calls are equivalent.

The best way is to pass as position arguments. Nothing else. You may pass as string, but this way may cause some troubles. Example:

array=(one two three four five)

function show_passed_array(){
  echo $@
}

or

function show_passed_array(){
  while $# -gt 0;do
    echo $1;shift
  done
}

    show_passed_array ${array[@]}

output:

  one two three four five

You mean if array value has space symbols you must quote elements first before pass for accessing value by index in function use $1 $2 $3 ... position parameters. Where index 0 -> 1, 1 -> 2,... To iterate access it is best to use always $1 and after Shift. Nothing additional is needed. You may pass arguments without any array like this:

show_passed_array one two three four five

bash media automatically builds an array from passed arguments that passed them to function and then you have position arguments. Furthermore when you write ${array[2]} you really write consequent argument one two three four and passed them to the function. So those calls are equivalent.

Source Link

best way to pass as position arguments. Nothing else. You may pass as string. But this way may some troubles. Example

array=(one two three for five)

function show_passed_array(){
  echo $@
}

or

function show_passed_array(){
  while $# -gt 0;do
    echo $1;shift
  done
}

    show_passed_array ${array[@]}

output:

  one two three for five

You mean if array value has space symbols you must quoit elements first before pass for asses value by index in function use $1 $2 $3 ... position parameters. Where index 0 -> 1, 1 -> 2,... To iterate access best use always $1 and after shift. Nothing additional needed. You may pass arguments without any array like that:

show_pased_array one two three for five

bash media automatically build array from passed arguments that passed them to function and then you have position arguments. Foremore when you write ${array[2]} you in real white consequent argument one two three four and passed them to function. So those calls are equivalent.