[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: let vs double-parentheses
From: |
Koichi Murase |
Subject: |
Re: let vs double-parentheses |
Date: |
Sun, 7 Mar 2021 02:15:53 +0800 |
2021年3月7日(日) 1:43 Peng Yu <pengyu.ut@gmail.com>:
> It is not clear what is the difference between let and
> double-parentheses. Can they be used interchangeably? Or in some cases
> that one can not replace the other?
They are basically the same, but there can be a small usage difference
coming from the syntactic difference between the normal command `let'
and the construct `((...))': One can specify multiple arithmetic
expressions to `let' but cannot specify them in `((...))'.
- For example, `let a=1 b=2 c=3' cannot be replaced by `((a=1 b=2 c=3))'.
- For another example, when one wants to process arguments as
arithmetic expressions, one may write something like `let "$@"' using
let. If one wants to do it with `((...))', one needs to combine it
with a for loop as `for expr; do ((expr)); done'.
- For a more practical example, when one wants to calculate
1+2+3+....+100, one can write `let a=0 a+={1..100}' using let, but
need to write `a=0; for ((i=1;i<100;i++)); do ((a+=i)); done' using
`((...))'.
> Of the two, which one is
> preferred? Thanks.
I think `((...))' is more preferred for usual cases because it is free
from some unwanted expansions like pathname expansions and word
splitting in the arithmetic context.
--
Koichi