0% found this document useful (0 votes)
59 views

Explain Mathematical Commands Used in Linux.

Uploaded by

hshshahs166
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Explain Mathematical Commands Used in Linux.

Uploaded by

hshshahs166
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Absolutely! Let's explore the mathematical commands you can wield within the Linux terminal.

Built-in Arithmetic in Bash

The Bash shell itself can handle basic arithmetic operations within double parentheses (( )) or
using the let command.

● Double Parentheses:
Bash
echo $((5 + 3)) # Output: 8
result=$((10 * 2))
echo $result # Output: 20

● let Command:
Bash
let result=15/3
echo $result # Output: 5

You can use standard operators like +, -, *, /, and % (modulo) for addition, subtraction,
multiplication, division, and remainder, respectively.

The expr Command

The expr command is another versatile tool for evaluating expressions. Remember to escape
operators like * with a backslash \ within expr.

Bash

expr 5 + 8 # Output: 13
expr 20 \* 4 # Output: 80

The bc Command (Basic Calculator)

bc is a powerful arbitrary-precision calculator language. It can handle floating-point calculations,


more complex functions, and even scripts.

Bash

echo "scale=2; 10 / 3" | bc # Output: 3.33


echo "sqrt(25)" | bc # Output: 5

The awk Command

awk is primarily a text-processing tool, but its ability to handle arithmetic makes it handy for
calculations within data streams.

Bash

echo 5 8 | awk '{print $1 + $2}' # Output: 13

Other Specialized Commands

● factor: Decomposes integers into prime factors.


Bash
factor 12 # Output: 12: 2 2 3

● dc: A reverse-polish notation calculator.


Bash
echo "5 8 + p" | dc # Output: 13

Important Considerations

● Integer Arithmetic: By default, most Linux arithmetic commands perform integer arithmetic.
If you need floating-point calculations, use bc or configure the scale variable in bc or awk.
● Escape Characters: Remember to escape special characters like * within expr using
backslashes.
● Command Substitution: You can embed arithmetic expressions within command
substitution $(()) to perform calculations directly within commands.

Feel free to ask if you have any specific calculations in mind or want to explore a particular
command in more detail. I'm here to help you master the mathematical prowess of your Linux
terminal!

You might also like