通过一条命令计算输出1+2+….+10的表达式,并计算出结果
方法有多种,如下:
-bash-4.1# echo seq -s "+" 10
=seq -s "+" 10|bc
1+2+3+4+5+6+7+8+9+10=55
-bash-4.1# echo echo {1..10}|tr " " "+"
=echo {1..10}|tr " " "+"|bc
1+2+3+4+5+6+7+8+9+10=55
-bash-4.1# echo seq -s "+" 10
=$[seq -s "+" 10
]
1+2+3+4+5+6+7+8+9+10=55
-bash-4.1# echo seq -s "+" 10
=$(expr $[seq -s "+" 10
])
1+2+3+4+5+6+7+8+9+10=55
-bash-4.1# echo seq -s "+" 10
=$((seq -s "+" 10
))
1+2+3+4+5+6+7+8+9+10=55
原文地址:https://blog.51cto.com/1009516/2427565