help-bash
[Top][All Lists]
Advanced

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

Re: Sequential binding when variable depends on values bound before it


From: Kerin Millar
Subject: Re: Sequential binding when variable depends on values bound before it
Date: Sat, 28 Jan 2023 16:39:41 +0000

On Sat, 28 Jan 2023 03:27:18 +0100 (CET)
Hans Lonsdale <hanslonsdale@mailfence.com> wrote:

> I have identified the problem with the following single line command
> 
> local  cl=40  cr=$((68-cl))
> 
> Because the arithmetic expression is evaluated before the variable 
> assignments, the value
> for cr does not use the set value for cl. 
> 
> Should this be considered a bug, so we can move on from such impasse ?

The problem is that, by electing to use the (built-in) local command, it 
becomes a simple command. As with any other simple command, the arithmetic 
expansion will be considered before the command is run.

Ordinary variable assignments work.

$ cl=40 cr=$((68-cl))
$ echo "$cr"
28

It follows that either of of these approaches will work.

local cl cr; cl=40 cr=$((68-cl))
local cl=40; local cr=$((68-cl))

-- 
Kerin Millar



reply via email to

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