T
T
trickster20192021-11-23 09:36:26
bash
trickster2019, 2021-11-23 09:36:26

How to add numbers from x to y in bash?

Good afternoon. According to the task, it is necessary to add the number from x to y that were entered by the user. And the only thing I thought of was to do the following.

#!/bin/bash
read -p "Type some number: " num
for((i=0;i<${#num};i++)); do ((sum+=${num:i:1})); done

echo "$sum

But there is a problem, all the numbers that will be summed up must be entered.
In this regard, the question is how to make it generate and add numbers?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xotkot, 2021-11-23
@trickster2019

9th grade, arithmetic progression

# b >= a
read -p "число 1: " a
read -p "число 2: " b
echo $[(a+b)*(b-a+1)/2]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question