Calculate Sum in Linux: Shell Script Guide

In Linux, you can calculate the sum of two numbers using a shell script. Simply create a script file with the calculation logic and execute it in the command line to get the result.

Here is a simple example of a shell script that can calculate the sum of two numbers:

#!/bin/bash

# 输入要计算的两个数
echo "Enter the first number:"
read num1

echo "Enter the second number:"
read num2

# 计算两个数的和
sum=$(($num1 + $num2))

# 输出结果
echo "The sum of $num1 and $num2 is: $sum"

Save the above code as a .sh file, then run the script file in the command line to get the sum of the two input numbers.

bannerAds