[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Why can redirection be combined in an assignment statement?
From: |
Andreas Kusalananda Kähäri |
Subject: |
Re: Why can redirection be combined in an assignment statement? |
Date: |
Mon, 10 May 2021 18:01:36 +0200 |
On Mon, May 10, 2021 at 10:40:47AM -0500, Peng Yu wrote:
> I can not see why redirection is allowed in an assignment statement.
> For example, redirection in the following command does not do anything
> useful.
>
> x=10 < /tmp/1.txt
>
> Is there a useful situation in which redirection does do something
> useful in an assignment statement? If not, why not prompt an error for
> this case?
>
> I can think of this, which basically truncate or create a new file
> /tmp/1.txt, and perform the assignment. But I don't see a point to
> combine both in a single line. Therefore, I would not consider this is
> as a useful situation.
>
> >/tmp/1.txt x=10
>
> --
> Regards,
> Peng
A redirection is allowed even though the redirected data is not read.
A redirection is allowed even though no data is transferred across the
stream.
A hypothetical scenario where you actually *want* redirection into an
assigment is when the assigment involves a command substitution (or
something similar) that reads the data:
$ 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
--
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden
.
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