Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 Developer Survey.

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.

0
votes
0answers
24 views

Any way to interpret string representing an integer or operation?

Lets say I have this string one plus two Is there any way we can interpret this as 1 + 2 faster than going into some conditions checking word per word and testing it if it equal something in a given ...
2
votes
2answers
85 views

String to integer in Shell

I'm currently working on a project called 'dmCLI' which means 'download manager command line interface'. I'm using curl to multi-part download a file. And I'm having trouble with my code. I can't ...
0
votes
2answers
54 views

When I add 0 to 0.0, how do I make sure the result is 0.0 not 0?

There are two columns of numbers in a file, the first line like this: 0 0.0 I want to add the numbers in column 1 to those in column 2, and I want to keep the results floats, not ...
1
vote
2answers
177 views

Date, arithmetic, and ternary operator in one line

I have a simple code to ensure a script takes at least x seconds (500 here) on Ubuntu t1=$(date +%s) # script is here t2=$(date +%s) let "t = 500 - $t2 + $t1" (( t = t>0 ? t : 1 )) sleep $t The ...
5
votes
2answers
761 views

Subtract time using bash?

Is it possible to use bash to subtract variables containing 24-hour time? #!/bin/bash var1="23:30" # 11:30pm var2="20:00" # 08:00pm echo "$(expr $var1 - $var2)" Running it produces the following ...
1
vote
0answers
19 views

AIX 6.1 versus AIX 7.1 strange behaviour [duplicate]

I have this logic in a ksh script on AIX 6.1 and it works fine. integer loop_count=0 integer sleep_interval=5 integer alarm_interval=60 if (( $loop_count*$sleep_interval%$alarm_interval -eq 0 )) then ...
5
votes
7answers
1k views

Do math operation on the numbers typed into command line without call bc

Sometimes I need to run some math operations. I know I can use bc or echo $(( 6/2 )). I have created my own function for bc to read input. But sometimes it takes a long time to type: _bc "6/2". So I ...
0
votes
3answers
184 views

Sum of command line arguments?

How would I create a bash script that the user can use to sum any amount of command line arguments, but it will also ignore incompatible arguments? For example, say my script is called sum: sum 10 -...
2
votes
1answer
70 views

bash arithmetic expansion seems to be prone to injection attacks

Suppose you want your script to take variables from environment: #!/usr/bin/env bash set -eu if (( ${A-} )); then echo true else echo false fi Arithmetic expansion seems to be more ...
-1
votes
3answers
34 views

Arithmetic sequence with files

i have two files,the first file is written numbers for example 123,and the second file is the same and i want to sum or divide file_one plus file_Two = 123 + 123 result=246 i want to make arithmetic ...
1
vote
1answer
182 views

awk arithmetic differs from expr

There are some contrast output between awk arithmetic and expr. Example expr 11111111111111111111 / 22 gives 505050505050505050 but with awk: echo '11111111111111111111' | awk '{q=$1/22;;print q}...
4
votes
4answers
76 views

Speed up script that determines if all columns in a row are the same or not

I need to speed up a script that essentially determines whether or not all the "columns" for each row are the same, then writes a new file containing either one of the identical elements, or a "...
1
vote
1answer
43 views

Is arithmetic expansion related to IFS in some way?

From https://unix.stackexchange.com/a/458074/674 arithmetic expansions are also subject to split+glob so should also be quoted (shift "$((OPTIND - 1))") (here not a problem though as you're using ...
2
votes
2answers
100 views

Multiplying numbers from each line in file is giving minus value

#!/bin/bash sed 's/[^0-9 ]*//g' $1 | tr " " "\n" > outfile.txt sed '/^\s*$/d' outfile.txt > outfile1.txt if [ $2 == "-s" ] || [ $2 == "-si" ] || [ $2 == "-is" ] then sum=0; while read num; ...
4
votes
4answers
39 views

How to print back the correct delimiter when using a regex expression as delimiter?

I have lines that look like this 123-456-789 12.34.56 example I want to select the 12, add 2 to it, then print the whole line as is. So the result should be : 123-456-789 14.34.56 example I have ...
0
votes
1answer
440 views

Display variable on dialog --msgbox

I want to output a arithmetic progression on a dialog message box, but the --msgbox attribute is showing them as plain text My code: while : do sequence=(1 10 100 1000 10000) dialog --stdout ...
3
votes
3answers
295 views

Why does $((0.1)) expand to 0.10000000000000001 in zsh?

In zsh: $ echo $((0.1)) 0.10000000000000001 While in other shells with floating point arithmetic expansion: $ ksh93 -c 'echo $((0.1))' 0.1 $ yash -c 'echo $((0.1))' 0.1 Or awk: $ awk 'BEGIN{print ...
0
votes
1answer
638 views

arithmetic operation inside Makefile rules

I need to perform an arithmetic operation inside a bash loop as explained below CYCLE?=3 COUNT=1 download_when_ready: ## Will try the download operations many times till it succeeds or it reaches 10 ...
7
votes
2answers
2k views

Can bash do floating-point arithmetic without using an external command? [duplicate]

I have read that bash can do integer arithmetic without using an external command, for example: echo "$((3 * (2 + 1)))" Can bash also do floating-point arithmetic without using an external command?
0
votes
2answers
322 views

sum 2 variables in a bash script within telnet

I am writing a script which should listen on a specific port and once somebody connects to it via that port (through telnet), it should ask for 2 numbers and return their sum. Like when I type telnet [...
1
vote
2answers
41 views

Arithmetic calculation row-wise for non-fixed column number

I have an input file with non-fixed column number on which I would like to do some arithmetic calculations on: input.txt ID1 4651455 234 4651765 392 4652423 470 ID2 16181020 ...
-2
votes
3answers
1k views

Subtracting date&time with format of yyyymmddhhmmss [closed]

I have two date/time values in yyyymmddhhmmss format in shell variables. I want to subtract one from the other and get the result in seconds like below: a=20170804020000 b=20170804015959 c=a-b The ...
5
votes
2answers
550 views

Shell exit silently at arithmetic expression in for loop

I have written a script to ssh remote hosts, execute commands, save output to files, and examine outputs. But it always exit silently at line (( success++ )) when iterate first item in array workers. ...
0
votes
1answer
149 views

Shell arithmetic: referencing a variable without the leading $

I was surprised to read this in the Bash manual, describing Arithmetic Evaluation: Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. ...
2
votes
1answer
55 views

How to make $((5/2)) deliver a floating point number? [duplicate]

This is what I get 🐷 echo $((5/2)) 2 How to make $((5/2)) giving me 2.5 ?
3
votes
5answers
887 views

arithmetic operations within column with awk or sed

I have a file whose columns contain simple arithmetic equations that I would like to merge to the arithmetic result. Input sample (tab-separated columns): +104-1+12 6 +3 I would like to ...
0
votes
2answers
990 views

Calculate sum of a column using shellscript

I have a csv file as text.csv and want to calculate the total salary of all employees. The data in it is as follows: Id,name,salary,designation 12,joe,thomas,253637,IT,admin 25,miacheal,364829,...
1
vote
2answers
664 views

Arithmetic syntax in bash

I am getting the below error on arithmetic values #!/bin/bash n=0 line_count=9 line_count=$(line_count)/3 echo $line_count exit 0 expected result is 3 []$ ./test.sh ./test.sh: line 4: line_count: ...
1
vote
1answer
565 views

Comparing 2 rows using awk

Input aaaaa|bbbbb|ccccc|ddddd|5|5|4 aaaaa|bbbbb|ccccc|ddddd|10|10|1 wwwww|xxxxx|yyyyy|zzzzz|5|5|2 wwwww|xxxxx|yyyyy|zzzzz|10|10|2 Output aaaaa|bbbbb|ccccc|ddddd|5|5| wwwww|xxxxx|yyyyy|zzzzz|10|10| ...
-3
votes
1answer
548 views

arithmetic syntax error when install shell script

When I trying to install a software license server which is a shell script, I have an error and really don't know the way out. I cannot find any error in the script. Hope anyone can give me some help. ...
8
votes
3answers
3k views

Addition of extremely large numbers in shell script

Suppose that two numbers are stored in two different files, a.txt and b.txt. Each number is large enough (more than 30 digits) to not be supported by the numeric data type used by bash. How may I ...
0
votes
3answers
776 views

Using awk to divide the number in each line of a file by the maximum value in that file

I have a file like this: 0.0451660231 0.0451660231 0.0527343825 0.3933106065 0.3970947862 0.0489502028 0.3592529595 0.3592529595 0.3592529595 0.3630371392 0.3630371392 0.3668213189 0.4008789659 0....
-2
votes
1answer
41 views

Generate between interval while having overflow

echo INPUT | MAGIC > OUTPUT INPUT: a random number that can be 0-999999999999 (so very big) OUTPUT: a number between: 0-1023 MAGIC: a solution where the random smaller/bigger input is "converted"...
14
votes
4answers
10k views

Bitwise shift and the largest integer in Bash

This is an exploration question, meaning I'm not completely sure what this question is about, but I think it's about the biggest integer in Bash. Anyhow, I'll define it ostensively. $ echo $((1<&...
1
vote
1answer
777 views

Divide items of a column by one-item column

How to iteratively divide the elements of a column by a one-element item in another column? Eg, Input: 2 2 2.2 4 44 Expected Output: 1 1.1 2 22 Standard division awk '{print $1/$2}' Input &...
5
votes
2answers
996 views

arithmetic operations between files

Say I have these two input files: > file1 2 3 4 > file2 10 100 1000 And I would like to compute: file1/file2 for each line, resulting in file3: > file 3 0.2 0.03 0.004 Divisions in bash ...
4
votes
1answer
14k views

“) Syntax error Invalid arithmetic operator (error token is ”

I have commands in a bash script getting data from a source text file and then adding the value of a variable to it and using it in an if/else condition. Source data file (db_count.ini) (Note: ...
1
vote
2answers
707 views

what's the simplest way to increment the numbers in this string 01:02:99 in unix?

What is the simplest way to increment the numbers in this string 01:02:99? Expected output: 01:03:00 I'm hoping for a sed response, but I don't think it would be simple.
1
vote
1answer
571 views

Why does the factor command produce nonsense on an RSA modulus?

Ok, so I think I may have found a flaw in... something. I honestly don't know. This is such a weird scenario. I was attempting to break very small asymmetric (RSA) encryption, just as a Proof of ...
0
votes
1answer
914 views

Bash script to add or subtract two numbers from user input

I am very new to Linux and am taking a course in it. Very excited to learn more. I am working on a script that asks user for two numbers, then asks whether user wants to add or subtract. I failed the ...
0
votes
1answer
67 views

How does this equation gives numbers random between two values?

How does this equation gives random numbers between max and min divisible by divisibleBy echo $(((RANDOM%(max-min+divisibleBy))/divisibleBy*divisibleBy+min))
-2
votes
1answer
700 views

Arithmetic expressions in for-loop

In for (( expr1 ; expr2 ; expr3 )) ; do commands ; done expr1, expr2, and expr3 are arithmetic expressions. Is expr1 ; expr2 ; expr3 not an arithmetic expression? (( expr1 ; expr2 ; expr3 )) isn't ...
13
votes
3answers
39k views

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 ...
1
vote
1answer
335 views

Arithmetic expansion and parameter expansion

$ x=foo; foo=bar; bar=6; $ echo $x foo $ echo $((x)) 6 why can the arithmetic expansion trace from variable x to its value foo, from variable foo to its value bar, and then from variable foo to ...
3
votes
1answer
1k views

Variables of integer type in Bash?

There are two ways to define and use variables of integer type in Bash declare -i a new variable use a variable in an arithmetic expression, without declaring it. My questions: What are the ...
-2
votes
1answer
513 views

Is a string an arithmetic expression?

In Bash, I use a string where an arithmetic expression is expected: $ (( hello )) $ echo $? 1 $ echo $(( hello )) 0 $ let aaa=hello $ echo $? 1 $ echo $aaa 0 There is no error reported. So is a ...
2
votes
1answer
231 views

Bash shell script program that prompts for and reads two integers from the user.

Okay, so I been trying to figure this problem for a while and I'm coming up with errors left and right. This is my first time trying to learn the basics. So any advice would be helpful! Problem: I'm ...
2
votes
2answers
804 views

Sed incremental arithmetic

I want to replace a number at the end of a string by incrementing the number by one each time I run a specific sed line. the string looks like this: server-port=25555 I was thinking I could run ...
2
votes
2answers
315 views

Why do newer versions of awk print large integers instead of decimals?

I often use the awk (gawk) calculator: calc(){ awk "BEGIN{ print $* }" ;} In Ubuntu 14.04, it output big numbers in standard form, e.g. $ calc 56777654409823*4674990588446667952594759939814064128 2....
4
votes
2answers
2k views

What does the comma operator do in Bash arithmetic?

In Bash Manual, sec 6.5 Shell Arithmetic expr1 , expr2 comma What does the comma operator do? Are expr1 and expr2 arithmetic expressions?