Questions tagged [arithmetic]

This tag is meant for everything that has to do with mathematical operations. So if you want to ask a question about doing calculations in `awk`, or if you have a question about `bc`, this is the right tag to use.

203 questions
409
votes
17 answers

How to do integer & float calculations, in bash or other languages/frameworks?

Using echo "20+5" literally produces the text "20+5". What command can I use to get the numeric sum, 25 in this case? Also, what's the easiest way to do it just using bash for floating point? For example, echo $((3224/3807.0)) prints 0 :(. I am…
Michael Durrant
  • 41,213
  • 69
  • 165
  • 232
77
votes
7 answers

Parenthesis in expr arithmetic: 3 * (2 + 1)

expr does not seem to like parenthesis (used in mathematics to explicit operator priority): expr 3 * (2 + 1) bash: syntax error near unexpected token `(' How to express operator priority in bash?
Nicolas Raoul
  • 7,945
  • 14
  • 43
  • 55
68
votes
6 answers

How to create a sequence with leading zeroes using brace expansion

When I use the following, I get a result as expected: $ echo {8..10} 8 9 10 How can I use this brace expansion in an easy way, to get the following output? $ echo {8..10} 08 09 10 I now that this may be obtained using seq (didn't try), but that…
Bernhard
  • 11,992
  • 4
  • 59
  • 69
65
votes
3 answers

Bash: double equals vs -eq

I am doing integer comparison in bash (trying to see if the user is running as root), and I found two different ways of doing it: Double equals: if [ $UID == 0 ] then fi -eq if [ $UID -eq 0 ] then fi I understand that there's no >= or <= in bash,…
beatgammit
  • 7,453
  • 10
  • 31
  • 32
47
votes
2 answers

What does a dollar sign followed by a square bracket $[...] mean in bash?

It appears that $[expr] performs arithmetic expansion just like $((expr)). But I can't find any mention of $[ in the bash manual. This command gives no results: gunzip -c /usr/share/man/man1/bash.1.gz | grep -E '\$\[' What is this operator and is…
AmadeusDrZaius
  • 1,232
  • 1
  • 12
  • 19
44
votes
3 answers

Comparing integers: arithmetic expression or conditional expression

In Bash, two integers can be compared using conditional expression arg1 OP arg2 OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to,…
Tim
  • 98,580
  • 191
  • 570
  • 977
43
votes
4 answers

How do I remove leading zeroes from output of 'date' or avoid octal interpretation of such decimal numbers?

I have this: date +"%H hours and %M minutes" I use festival to say it up.. but it says like: "zero nine hours".. I want it to say "nine hours"! but date always give me 09... so I wonder if bash can easly make that become just 9? in the complex…
Aquarius Power
  • 4,099
  • 5
  • 38
  • 56
42
votes
5 answers

creating a sequence of numbers, one per line in a file

Is there a way to create out of thin air, a file that is a sequence of numbers, starting at a given number, one per line? something like magic_command start 100 lines 5 > b.txt and then, b.txt would be 100 101 102 103 104
Duck
  • 4,434
  • 19
  • 51
  • 64
41
votes
11 answers

How to compare two floating point number in a shell script

I want to compare two floating point numbers in a shell script. The following code is not working: #!/bin/bash min=12.45 val=10.35 if (( $val < $min )) ; then min=$val fi echo $min
RIchard Williams
  • 695
  • 1
  • 9
  • 12
40
votes
6 answers

How to add arithmetic variables in a script

I want to accumulate the line size of a number of files contained in a folder. I have written the following script: let a=0 let num=0 for i in folder/* do num=`cat $i | wc -l` a=$a+$num done echo $a What i am geting at the end of…
curious
  • 629
  • 1
  • 7
  • 10
34
votes
8 answers

Doing simple math on the command line using bash functions: $1 divided by $2 (using bc perhaps)

Sometimes I need to divide one number by another. It would be great if I could just define a bash function for this. So far, I am forced to use expressions like echo 'scale=25;65320/670' | bc but it would be great if I could define a .bashrc…
ixtmixilix
  • 13,040
  • 27
  • 82
  • 118
32
votes
7 answers

Binary to hexadecimal and decimal in a shell script

I have a context where I need to convert binary to hexadecimal and decimal and viceversa in a shell script. Can someone suggest me a tool for this?
Bangi
  • 429
  • 1
  • 5
  • 7
31
votes
5 answers

Check if $REPLY is in a range of numbers

I'm writing a shell script for Linux, using Bash, to translate any video-file into a MP4. For that, I'm using avconv with libvorbis for audio. Inside my script, I have a question for the user : read -p "- Audio Quality [scale from -2 to 10] ? " …
MrVaykadji
  • 533
  • 1
  • 6
  • 12
30
votes
4 answers

bash -e exits when let or expr evaluates to 0

I have a bash script that sets -e so the script will exit on any exit status != 0. I'm trying to do some basic shell arithmetic assigned to variables and sometimes the expression equals 0 which causes the exit status of the let or expr command to be…
Dougnukem
  • 403
  • 4
  • 6
30
votes
1 answer

Security Implications of using unsanitized data in Shell Arithmetic evaluation

In a comment to a recent question, Stéphane Chazelas mentions that there are security implications to double parentheses arithmetic such as: x=$((1-$x)) on most shells. My Google skills seem to be rusty and I can't find anything. What are the…
garethTheRed
  • 33,289
  • 4
  • 92
  • 101
1
2 3
13 14