Search code examples
arraysbashlocal

Declaring arrays local to a function


I am declaring an array inside a function using the declaration

declare -a r

I need to employ a local array to the function.

Would I require

local declare -a r

Solution

  • I need to employ a local array to the function.

    The variable is already local. declare is exactly the same as local. The is no difference.

    declare -a arr
    # exactly the same as
    local -a arr
    

    If you want to visually distinguish that the variable is local, then use local keyword.

    If you want the variable to be global, use -g switch. See bash manual.

    Would I require local declare -a r

    No, that would declare a variable named declare.