Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to perform arithmetic operation in Linux terminal

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to perform arithmetic operations in Linux terminals? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

1. Use Bash Shell

The first and easiest way to perform basic mathematical operations on Linux CLI is to use double parentheses. Here are some examples of how we use values stored in variables:

ADD=$ ((1 + 2))

$echo $ADD

$MUL=$ (($ADD * 5))

$echo $MUL

$SUB=$ ($MUL-5)

$echo $SUB

$DIV=$ ($SUB / 2))

$echo $DIV

$MOD=$ (($DIV 2))

$echo $MOD

Algorithms in Linux Bash Shell

two。 Use the expr command

The expr command evaluates the expression and prints the value of the provided expression to standard output. We'll look at different ways to use expr for simple math, compare, increase the value of a variable, and find the length of a string.

Here are some examples of simple calculations using the expr command. Note that many operators need to escape or reference shell, such as the * operator (we'll see more in the comparison of expressions).

$expr 3 + 5

$expr 15 3

$expr 5\ * 3

$expr 5-3

$expr 20 / 4

The basic algorithm of using expr command in Linux

Next, we will show you how to make a comparison. When the expression evaluates to false, expr prints a value of 0, otherwise it prints 1.

Let's look at some examples:

$expr 5 = 3

$expr 5 = 5

$expr 8! = 5

$expr 8\ > 5

$expr 8\ < 5

$expr 8\

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report