[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: declare -p doesn't show the global attribute
From: |
isabella parakiss |
Subject: |
Re: declare -p doesn't show the global attribute |
Date: |
Mon, 27 Apr 2015 08:26:33 +0200 |
On 4/27/15, Chet Ramey <chet.ramey@case.edu> wrote:
> On 4/26/15 5:26 PM, isabella parakiss wrote:
>> $ fn () { declare -g var=x; declare -p var; } ; fn
>> declare -- var="x"
>>
>> I think the correct output should be declare -g var="x"
>> Is this intended or is it a bug?
>
> There is no such thing as `the global attribute'. The -g option simply
> causes declare to create variables at the global scope instead of in a
> function-local scope. The output you see is no different than what
> would have been displayed had `var' been declared and given a value at
> the global scope outside the function.
>
Ok I understand, but would it be possible to add it?
My use case may not be the best possible example, but I am sourcing a
"library" to set various parts of my interactive shells. In this library
there's a function that sets the color variables from the output of tput.
Trying to avoid to call tput when it's not needed, I decided to save them
to an external file and source it if it exists. My problem is that if I
source that file from the function, I'm setting local variables. This is
the part that sets those variables:
https://gist.githubusercontent.com/izabera/d8f0f63fa3dc168af7b9/raw/581753078d33acea3d28b005722edb42c717fa96/gistfile1.sh
It seems to me that if declare -p showed the -g, I could just set them as
global and readonly before I save them, and I wouldn't need to set them
after I source them, in line 6.
Instead of declare -p, one could use something like printf %s=%q but it's
kinda hard to save arrays that way...
It's not a big deal, I just think it would make more sense.
---
xoxo iza