bug-bash
[Top][All Lists]
Advanced

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

Re: Errors are ignored with local variable assignment


From: Greg Wooledge
Subject: Re: Errors are ignored with local variable assignment
Date: Wed, 4 Jan 2023 10:12:18 -0500

On Wed, Jan 04, 2023 at 12:21:27AM -0600, Carlos Prieto López wrote:
> Description:
>         Errors are ignored when a command with error is assigned to a local
> variable on the same line of declaration.
> 
> f(){
> return 1
> }
> 
> g(){
> local v=`f`
> }

> g && echo g

> the output is 'g', but I believe there should be no output

The "local" command has its own exit status, and this masks the exit
status of the command substitution.

You've already found a workaround:

> i(){
> local v
> v=`f`
> }

Just continue using that.  This isn't a bug in bash, and it won't be
changed.

(While you're at it, you might want to switch from the obsolete backtick
syntax to the preferred $() syntax for command substitutions.)



reply via email to

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