help-bash
[Top][All Lists]
Advanced

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

Re: How to test if a variable is declared?


From: Peng Yu
Subject: Re: How to test if a variable is declared?
Date: Fri, 13 Mar 2020 09:23:39 -0500

No. It is not for an embedded. The long string is just a hypothetical case.
I don’t want to introduce this kind of performance buggy code only to later
find it cause real problems. It is better to avoid them in the first place.

To give you a concrete example, I previously used $() a lot and only later
find that it was a performance bottleneck. It took a lot of time to replace
them with something lighter-weight. Therefore, my philosophy is to be sure
about the performance as much as possible before using it widely in my code.

On Fri, Mar 13, 2020 at 9:04 AM Andreas Kusalananda Kähäri <
address@hidden> wrote:

> On Fri, Mar 13, 2020 at 07:55:55AM -0500, Peng Yu wrote:
> > This still prints. It just got redirected to null. So when the variable
> > contains a long string, it will use extra CPU for a test that could be
> made
> > more light weight. So there is no way to just test if a variable is
> > declared in bash?
>
>
> Could you say something about your actual real-life use case?  Are you,
> for example, developing in bash on some severely restricted embedded
> system?  From where does your very long string come from?  What is the
> situation in which you need to make a decision based on this particular
> test?
>
>
>
> >
> > On Fri, Mar 13, 2020 at 6:33 AM Andreas Kusalananda Kähäri <
> > address@hidden> wrote:
> >
> > > On Fri, Mar 13, 2020 at 06:15:16AM -0500, Peng Yu wrote:
> > > > This involves printing something to the screen. Is there something
> along
> > > > the line of [[ that doesn’t print to screen? Thanks.
> > >
> > > if declare -p foo 2>/dev/null; then
> > >    # foo is declared
> > > else
> > >    # foo is not declared
> > > fi
> > >
> > >
> > >
> > > >
> > > > On Thu, Mar 12, 2020 at 10:07 PM David <address@hidden>
> wrote:
> > > >
> > > > > On Fri, 13 Mar 2020 at 13:59, Peng Yu <address@hidden> wrote:
> > > > >
> > > > > > What is the best way
> > > > > > to test whether a viable is declared?
> > > > >
> > > > > Here's one way:
> > > > >
> > > > > $ echo "$BASH_VERSION"
> > > > > 4.4.12(1)-release
> > > > > $ unset v
> > > > > $ declare -p v ; echo $?
> > > > > bash: declare: v: not found
> > > > > 1
> > > > > $ declare v
> > > > > $ declare -p v ; echo $?
> > > > > declare -- v
> > > > > 0
> > > > > $
> > > > >
> > > > > --
> > > > Regards,
> > > > Peng
> > >
> > > --
> > > Andreas (Kusalananda) Kähäri
> > > SciLifeLab, NBIS, ICM
> > > Uppsala University, Sweden
> > >
> > > .
> > >
> > --
> > Regards,
> > Peng
>
> --
> Andreas (Kusalananda) Kähäri
> SciLifeLab, NBIS, ICM
> Uppsala University, Sweden
>
> .
>
-- 
Regards,
Peng


reply via email to

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