bug-bash
[Top][All Lists]
Advanced

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

Re: syntax error when setting variable in front of "while"


From: mfwitten
Subject: Re: syntax error when setting variable in front of "while"
Date: Sun, 08 Aug 2010 19:17:56 -0700 (PDT)

On Fri, Aug 6, 2010 at 5:40 AM, Ralf Goertz wrote:

>> Bash Version: 4.0
>> Patch Level: 35
>> Release Status: release
>>
>> Description:
>>
>> I am used to setting variables in front of a command
>> like in
>>
>>> LANG=C man mplayer
>>
>> However, I get a "bash: syntax error near unexpected
>> token `do'" error when trying:
>>
>>> FOO=BAR while read a b; do echo $a $b; done
>>
>> Why? The similar
>>
>>> FOO=BAR read a b && echo $a $b
>>
>> works fine. (The problem occurred when I tried to
>> temporarily set IFS to the tab character for a "read").

On Fri, 2010-08-06 13:16:57 -0500, Dennis Williamson wrote:

> while is a compound command. Only simple commands can
> have preceding variable assignments. From man bash:
>
> The environment for any simple command or function may
> be augmented temporarily by prefixing it with parameter
> assignments, as described above in PARAMETERS. These
> assignment statements affect only the environment
> seen by that command.

At first glance, it seems to me that this is an arbitrary
distinction; would there not be a benefit in allowing
compound commands to serve in many of the same contexts
as simple commands?

For instance, according to:

  info bash 'Basic Shell Features' 'Shell Commands' 'Compound Commands'

we already have:

  Any redirections associated with a compound command
  apply to all commands within that compound command
  unless explicitly overridden.

Thus (with `process subsitution' available):

  $ { read f; read g; printf "$f\n===\n$g\n"; } < <(printf 'Hello\nWolrd!\n')
  Hello
  ===
  Wolrd!

Wouldn't it be useful to extend other constructs to
compound commands?

Interestingly, according to:

  info bash 'Basic Shell Features' 'Shell Commands' Pipelines

we have:

  A `pipeline' is a sequence of simple commands separated
  by one of the control operators `|' or `|&'.

and according to:

  info bash 'Basic Shell Features' 'Shell Commands' Lists

we have:

  A `list' is a sequence of one or more pipelines
  separated by one of the operators `;', `&', `&&', or
  `||', and optionally terminated by one of `;', `&', or a
  `newline'.

However, compound commands already seem to work in these
contexts:

  $ printf 'Hello\nWolrd!\n' | { read f; read g; printf "$f\n===\n$g\n"; }
  Hello
  ===
  Wolrd!

and:

  $ { false; } && Hmmmm || echo OK
  OK

At least the bash documentation should be updated
to reflect these facts.

Sincerely,
Michael Witten



reply via email to

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