help-bash
[Top][All Lists]
Advanced

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

Re: Why can redirection be combined in an assignment statement?


From: Koichi Murase
Subject: Re: Why can redirection be combined in an assignment statement?
Date: Tue, 11 May 2021 01:41:13 +0900

2021年5月11日(火) 0:41 Peng Yu <pengyu.ut@gmail.com>:
> I can not see why redirection is allowed in an assignment statement.
> For example, redirection in the following command does not do anything
> useful.

I think it's just because the syntactic rule becomes simpler, i.e., it
is easier to explain the rule to users than disallowing or allowing
each combination separately.

It is not the reason for allowing variable assignments combined with
redirections, but we may think of the following use cases:

  some-condition &&
    x=$(something1) z=$(something2) > tmpfile

If we need to always separate unrelated simple command elements with
`;' or new lines, we need to write

  some-condition &&
    { x=$(something1); z=$(something2); > tmpfile; }

Another use case might be

  tmpfile=$RANDOM.tmp > "$tmpfile"

to create an empty temporary file with its name assigned to a variable
`tmpfile'. But again, these are not the original motivation of
allowing just combining variable assignments and redirections.



reply via email to

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