1

I am learning bash. Now I would like to give an empty array to a function. However, it does not work. Please refer to following,

function display_empty_array_func() {
  local -a aug1=$1
  local -a aug2=${2:-no input}
  echo "array aug1 = ${aug1[@]}"
  echo "array aug2 = ${aug2[@]}"
}

declare -a empty_array=()

display_empty_array_func "${empty_array[@]}" "1"

The outputs are following,

array aug1 = 1  # I expected it is empty
array aug2 = no input  # I expected it is 1

In my understanding, quoting variable allow us to give an empty variable, like following,

function display_empty_variable_func() {  
  local aug1=$1
  local aug2=${2:-no input}
  echo "variable aug1 = ${aug1}"
  echo "variable aug2 = ${aug2}"
}

display_empty_variable_func "" "1"

And its outputs are following

variable aug1 =    # it is empty as expected
variable aug2 = 1  # it is 1 as expected

I don't know what is the problem with passing am empty array. Someone who knows mechanism or solutions. Please let me know it. Thank you very much.

mora
  • 2,027
  • 3
  • 15
  • 30
  • 2
    this might help: https://stackoverflow.com/questions/16461656/bash-how-to-pass-array-as-an-argument-to-a-function – Sundeep Nov 14 '16 at 10:14

1 Answers1

0

enter image description hereIf positional parameter is empty, shell script & shell function will not consider that value and instead it took the next non empty value as its value(positional parameter value). If we want to take the empty value, must we have to put that value in quotes.

Ex: I'm having small script like 
cat test_1.sh
#!/bin/bash

echo "First Parameter is :"$1;

echo "Second Parameter is :"$2;

case -1
If i executed this script as 
sh test_1.sh

First Parameter is :

Second Parameter is :

Above two lines are empty because i have not given positional parameter values to script.

case-2
If i executed this script as 
sh test_1.sh 1 2

First Parameter is :1

Second Parameter is :2

case-2
If i executed this script as 
sh test_1.sh  2 **# here i given two spaces in between .sh and 2 and i thought 1st param is space and second param is 2 but**

First Parameter is :2

Second Parameter is :

The output looks like above. My first statement will apply here.

case-4
If i executed this script as 
sh test_1.sh " " 2

First Parameter is :

Second Parameter is :2

Here i kept space in quotes. Now i'm able to access the space value.

**This will be help full for you. But for your requirement please use below code.** 

In this you have **quote** the space value(""${empty_array[@]}"") which is coming from **an empty array**("${empty_array[@]}"). So you have to use additional quote to an empty array.

function display_empty_array_func() {
  local -a aug1=$1
  local -a aug2=${2:-no input}
  echo "array aug1 = ${aug1[@]}"
  echo "array aug2 = ${aug2[@]}"
}

declare -a empty_array=()

display_empty_array_func ""${empty_array[@]}"" "1"

The output is:
array aug1 =
array aug2 = 1
learner
  • 111
  • 1
  • 2
  • 12
  • thank you for answering. I copied your sample and test it but it did not change the result. How did you run your code? Thank you very much. – mora Nov 14 '16 at 15:56
  • it is working as i explained. For your ref PFA screenshot. – learner Nov 15 '16 at 10:30
  • thank you for answering again. It still does not work in my environment; GNU bash, version 4.3.42. But please don't mind this question any more. I referred to other site which Sundeep recommended above. – mora Nov 17 '16 at 09:52