[Top][All Lists]

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

Re: Arithmetic expansion with increments and output redirection

From: Ilkka Virta
Subject: Re: Arithmetic expansion with increments and output redirection
Date: Wed, 24 Apr 2019 16:49:35 +0300
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:60.0) Gecko/20100101 Thunderbird/60.6.1

On 24.4. 16:37, Chet Ramey wrote:
"Utilities other than the special built-ins (see Special Built-In
Utilities) shall be invoked in a separate environment that consists of the
following...[includes redirections specified to the utility]...

It does say

"Open files inherited on invocation of the shell, open files controlled by the exec special built-in plus any modifications, and additions specified by any redirections to the utility"

which could also be read to apply only the open files themselves, not the byproducts of finding out their names.

environment of the shell process shall not be changed by the utility"

It's not the utility that changes the environment when processing the expansion, but the shell itself, isn't it?

- -

Anyway, as little as it's worth, Zsh seems to do it the same way Bash does, all others leave the changed value visible.

 $ for shell in 'busybox sh' dash yash ksh93 mksh bash zsh; do $shell -c
   'i=1; /bin/echo foo > $(( i += 1 )); printf "%-15s %s\n" "$1:" "$i";'
   sh "$shell"; done
 busybox sh:     2
 dash:           2
 yash:           2
 ksh93:          2
 mksh:           2
 bash:           1
 zsh:            1

I also did find the Bash/Zsh behaviour a bit surprising. But I'm not sure it matters other than here and with stuff like $BASHPID? It's easy to work around here by splitting the increment/decrement to a separate line:

 /bin/echo foo > "$i"
 : "$(( i += 1 ))"

Some find that easier to read, too: the increment isn't "hidden" within the other stuff on the command line.

Ilkka Virta / address@hidden

reply via email to

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