[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 14:33:11 -0400 |
On 5/10/21 11:40 AM, 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
$ readonly ro_var=one
$ rw_var=two
$ ro_var=hohoho >/tmp/1.txt
bash: ro_var: readonly variable
$ file /tmp/1.txt
/tmp/1.txt: cannot open `/tmp/1.txt' (No such file or directory)
$ rw_var=hmmm >/tmp/1.txt
$ file /tmp/1.txt
/tmp/1.txt: empty
Redirection from the file into the context of a simple variable
assignment without the previously mentioned command substitution, might
not seem to do something. But redirection *to* the file, does do
something. It creates a file, unless the command line has a bash error.
You may of course debate whether it is useful, but it certainly does
*something*.
> 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
Combining both into a single line might be useful if, for some reason,
some script writer really needed that file truncated/created only if the
variable assignment succeeded. Personally, I don't find this useful
myself either, but the world is a large place...
--
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 <=
Re: Why can redirection be combined in an assignment statement?, Chet Ramey, 2021/05/10