在不使用 seq 的情况下获取 bash 中的数字序列
Get sequence of numbers in bash without using seq
我想遍历 BASH 脚本中的数字序列,但绑定值不是常量。我知道这个语法:
for year in {2000..2010}; do
echo ${year}
done
但是在我的例子中,2000 年和 2010 年的值正在发生变化,所以我现在正在做的是:
for year in `seq ${yeari} ${yeare}`; do
echo ${year}
done
是否有一种 bash 原生方法可以在不使用 seq
的情况下执行相同的操作?
看看man bash
:
for (( expr1 ; expr2 ; expr3 )) ; do list ; done
First, the arithmetic expression expr1 is evaluated according to
the rules described below under ARITHMETIC EVALUATION. The
arithmetic expression expr2 is then evaluated repeatedly until
it evaluates to zero. Each time expr2 evaluates to a non-zero
value, list is executed and the arithmetic expression expr3 is
evaluated. If any expression is omitted, it behaves as if it
evaluates to 1. The return value is the exit status of the last
command in list that is executed, or false if any of the expres-
sions is invalid.
for ((i=yeari; i<yeare; i++))
do
echo $i
done
您可能还对 'Arithmetic Expansion' ($((expression))
) 感兴趣,您可以在手册页中找到更多信息。
我想遍历 BASH 脚本中的数字序列,但绑定值不是常量。我知道这个语法:
for year in {2000..2010}; do
echo ${year}
done
但是在我的例子中,2000 年和 2010 年的值正在发生变化,所以我现在正在做的是:
for year in `seq ${yeari} ${yeare}`; do
echo ${year}
done
是否有一种 bash 原生方法可以在不使用 seq
的情况下执行相同的操作?
看看man bash
:
for (( expr1 ; expr2 ; expr3 )) ; do list ; done First, the arithmetic expression expr1 is evaluated according to the rules described below under ARITHMETIC EVALUATION. The arithmetic expression expr2 is then evaluated repeatedly until it evaluates to zero. Each time expr2 evaluates to a non-zero value, list is executed and the arithmetic expression expr3 is evaluated. If any expression is omitted, it behaves as if it evaluates to 1. The return value is the exit status of the last command in list that is executed, or false if any of the expres- sions is invalid.
for ((i=yeari; i<yeare; i++))
do
echo $i
done
您可能还对 'Arithmetic Expansion' ($((expression))
) 感兴趣,您可以在手册页中找到更多信息。