[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Why can redirection be combined in an assignment statement?
From: |
Eli Schwartz |
Subject: |
Re: Why can redirection be combined in an assignment statement? |
Date: |
Mon, 10 May 2021 15:21:53 -0400 |
On 5/10/21 2:33 PM, Andreas Kusalananda Kähäri wrote:
> On Mon, May 10, 2021 at 11:16:59AM -0500, Peng Yu wrote:
>>> $ reverse=false; if "$reverse"; then a=$(rev); else a=$(cat); fi
>>> <<<"hello"; echo "$a"
>>> hello
>>> $ reverse=true; if "$reverse"; then a=$(rev); else a=$(cat); fi
>>> <<<"hello"; echo "$a"
>>> olleh
>>
>> I am talking about combining just assignment and redirection, which
>> comes from these rules.
>>
>> simple_command_element: WORD
>> | ASSIGNMENT_WORD
>> | redirection
>> ;
>>
>> simple_command: simple_command_element
>> | simple_command simple_command_element
>> ;
>>
>> Your example is not about this case. Therefore, it does not serve the
>> purpose of justifying combining assignment and redirection.
>
> Hm... I think that
>
> a=$(cat) <<<"hello"
>
> is exactly this. You may replace <<<"hello" by <somefile if you wish.
Outside of the if/else/fi, cat waits until you enter CTRL+D, then quits
and emits nothing; $a is "".
This works though:
{ a=$(cat); } <<< "hello"
But the redirection is being read by the {} group command in order to
let the variable assignment access it.
--
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User
OpenPGP_signature
Description: OpenPGP digital signature
Re: Why can redirection be combined in an assignment statement?, Koichi Murase, 2021/05/10
Re: Why can redirection be combined in an assignment statement?, Eli Schwartz, 2021/05/10
Re: Why can redirection be combined in an assignment statement?, Chet Ramey, 2021/05/10