help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] declare -g -a array doing different things


From: Bill Gradwohl
Subject: [Help-bash] declare -g -a array doing different things
Date: Fri, 30 Mar 2012 21:15:29 -0600

Below please find two almost identical scripts - version1 & version2 and their output.

Here is a diff report :
address@hidden ycc# diff version*
5c5,6
< declare -g -a array=(function)
---
> declare -g -a array
> array=(function)


Why the different output?

Version1 appears to be creating a local array, not a global array.

The non array variable is treated correctly in both.


address@hidden ycc# cat version1
#!/bin/bash

func1(){
declare -g variable='function'
declare -g -a array=(function)
echo  ${variable} address@hidden
}

declare -g variable='main'
declare -g -a array=(main)
echo  ${variable} address@hidden
func1
echo  ${variable} address@hidden

address@hidden ycc# ./version1
main main
function function
function main

address@hidden ycc# cat version2
#!/bin/bash

func1(){
declare -g variable='function'
declare -g -a array
array=(function)
echo  ${variable} address@hidden
}

declare -g variable='main'
declare -g -a array=(main)
echo  ${variable} address@hidden
func1
echo  ${variable} address@hidden

address@hidden ycc# ./version2
main main
function function
function function



--
Bill Gradwohl


reply via email to

[Prev in Thread] Current Thread [Next in Thread]