[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: bash optimization away question
From: |
Greg Wooledge |
Subject: |
Re: bash optimization away question |
Date: |
Sun, 3 Nov 2024 16:31:19 -0500 |
On Sun, Nov 03, 2024 at 22:21:31 +0100, #!microsuxx wrote:
> On Sun, Nov 3, 2024, 21:03 Chet Ramey <chet.ramey@case.edu> wrote:
>
> > On 11/2/24 8:29 PM, #!microsuxx wrote:
> > > is a
> > >
> > > var=${var:-blabla}
> > >
> > > optimized away to no exec at all
> > > if var is not empty
> > > ?
> >
> > No. The expansion has to take place, to account for side effects, and the
> > attempted assignment has to take place, since that can have side effects
> > and transform the value to be assigned as well. It doesn't make sense to
> > try and preserve the information about which variable was expanded for
> > this corner case.
> >
>
> .. so if a var is 10mb big
> and id do a couple of plain useless var=$var simply
> will it take much cpu , or nearly none ?
If this is a concern, then don't write your script that way. Do it a
simpler way:
[[ $var ]] || var=blabla
And if even *that* is too slow, then write your program in a different
language. Bash is many things, but fast isn't one of them.