Home > Net >  Declaring arrays local to a function
Declaring arrays local to a function

Time:12-15

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

CodePudding user response:

local replaces declare. Use declare for global scope and local for local scope. But do not use both in the same line.

CodePudding user response:

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.

CodePudding user response:

I would do a

local r
r=()

or

local r
declare -a r

or in one go

local r=()
  • Related