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

What are the terminal operators of Linux and how to use them

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what are the terminal operators of Linux and how to use them". In the daily operation, I believe many people have doubts about what the terminal operators of Linux are and how to use them. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about what and how to use the terminal operators of Linux. Next, please follow the editor to study!

The operators commonly used in Linux terminals are bash, let command, expr command, bc command awk command factor command and so on.

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\

Compare arithmetic expressions in Linux

You can also use the expr command to increase the value of the variable. Take a look at the following example (in the same way, you can also reduce the value of a variable).

$NUM=$ ((1 + 2)) $echo $NUM$ NUM=$ (expr $NUM + 2) $echo $NUM

Increment value of variable

Let's take a look at how to find the length of a string using the following methods:

$expr length "this is Linux Commune www.linuxidc.com"

For more information about the meaning of the above operators, see the expr man page:

$man expr3. Use the bc command

Bc (basic Calculator) is a command-line utility that provides all the functionality you want from a simple scientific or financial calculator. It is especially useful for performing floating-point mathematical operations. If the bc command is not installed, you can install it using the following command:

$sudo apt install bc # Debian/Ubuntu$ sudo yum install bc # RHEL/CentOS$ sudo dnf install bc # Fedora 22 +

After installation, you can run it in interactive mode or non-interactive mode by passing parameters to it-we'll look at both. To run it interactively, type the command bc at the command prompt and start doing some math, as shown in the figure.

$bc

Start bc in non-interactive mode

The following example shows how to use bc non-interactively on the command line.

$echo '3percent 5' | bc$ echo' 15% 2' | bc$ echo'15 / 2' | bc$ echo'(6 * 2)-5' | bc

The-l flag is used for the default scale (the number after the decimal point) to 20, for example:

Linuxidc@linuxidc:~/www.linuxidc.com$ echo '13swap 5' | bc2linuxidc@linuxidc:~/www.linuxidc.com$ echo' 13Accord 5' | bc-l2.60000000000000000000

Do math with floating numbers

4. Use the Awk command

Awk is one of the most famous text processors in GNU/Linux. It supports addition, subtraction, multiplication, division, and modular operators. It is also useful for floating-point mathematical operations. You can use it to perform basic mathematical operations, as shown in the figure.

$awk 'BEGIN {a = 6; b = 3; print "(a + b) =", (a + b)}' $awk 'BEGIN {a = 6; b = 3; print "(a-b) =", (a-b)}' $awk 'BEGIN {a = 6; b = 3; print "(a * b) =", (a * b)}' $awk 'BEGIN {a = 6; b = 3 Print "(a / b) =", (a / b)}'$awk 'BEGIN {a = 6; b = 3; print "(a% b) =", (a% b)}' 5. Use the factor command

The factor command is used to decompose integers into prime factors. For example:

Linuxidc@linuxidc:~/www.linuxidc.com$ factor 1818: 23 3linuxidc.com factor 133133: 7 19linuxidc.com linuxidc.com factor 2323: 23linuxidc.com factor 200200: 2225 this is the end of the study on "what are the terminal operators of linuxidc.com and how to use them". The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report