help-bash
[Top][All Lists]
Advanced

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

Re: Sourcing array declarations from a function


From: Eli Schwartz
Subject: Re: Sourcing array declarations from a function
Date: Thu, 26 Nov 2020 12:29:20 -0500

On 11/26/20 12:17 PM, Marco Ippolito wrote:
I would not expect this asymmetry of visibility, what explains it?

#! /usr/bin/env bash

cat > /tmp/file.bash << 'EOF'
parameter=foo
declare -A bar=([baz]=quux)
EOF

f() {
     source /tmp/file.bash
}

f

Inside of a function, you used declare without -g


echo "parameter: $parameter"
echo "bar's values: ${bar[@]}"

source /tmp/file.bash

echo "parameter: $parameter"
echo "bar's values: ${bar[@]}"

Outside of the function, you tried to access the `declare`d variable.

=== >

parameter: foo
bar's values:
parameter: foo
bar's values: quux

--
Marco Ippolito
maroloccio@gmail.com



--
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User

Attachment: OpenPGP_signature
Description: OpenPGP digital signature


reply via email to

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