Skip to main content
edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 267
Source Link

Loop function with arguments in another loop function with arguments

# Print $1 $2 times
function foo() {
    for (( i=0; i<$2; i++ )); do
        echo -n $1
    done
    echo
}

# Print $1 $2x$3 times
function bar() {
    for (( i=0; i<$3; i++ )); do
        foo $1 $2
    done
}

bar $1 $2 $3

The ideal output of foobar.sh @ 3 3 is

@@@
@@@
@@@

but the actual output seems to be just

@@@

Changing the variable in bar() from i to j yields the desired output. But why?