2

Please find my script and the output

#!/bin/bash
verify=("Tom Dick Harry" "Ricky" "Deep Rising")

function verifyServices()
{
        param=("${!1}")
        for i in ${param[@]}
        do
                echo $i
        done
}

verifyServices verify[@]

Output :-

Tom

Dick

Harry

Ricky

Deep

Rising

Expected output :-

Tom Dick Harry

Ricky

Deep Rising

Why? Is my array declaration wrong?

0

1 Answer 1

4

Use quotes inside the function:

function verifyServices() {
    param=("${!1}")
    for i in "${param[@]}"; do
       echo "$i"
    done
}

verifyServices verify[@]
Tom Dick Harry
Ricky
Deep Rising

Main problem in your code is this line:

for i in ${param[@]}

Due to spaces for loop is considering them as separate arguments.

It should be:

for i in "${param[@]}"
2
  • ohh.. these mistakes hurts
    – user2431227
    Commented Feb 19, 2014 at 11:54
  • 1
    Yes in shell scripts quoting can make a big difference.
    – anubhava
    Commented Feb 19, 2014 at 12:01