Stack Exchange Network

Stack Exchange network consists of 174 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

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.

6
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
87 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
0answers
44 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
27 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
83 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
73 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 "...
0
votes
1answer
38 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
84 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
38 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
300 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
231 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
373 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
1k 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
3answers
236 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
39 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 ...
4
votes
2answers
452 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
120 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
51 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
775 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
810 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
515 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
493 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
498 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
2k 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
652 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"...
13
votes
4answers
8k 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
607 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
846 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
12k 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
633 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
557 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
806 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
66 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
597 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 ...
10
votes
3answers
33k 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
265 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
416 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
185 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 ...
1
vote
2answers
524 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
224 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?
1
vote
2answers
292 views

Does command substitution within arithmetic substitution get word split?

I seem to recall from comments on this site that the contents of arithmetic expansion may be word split, but I can't find the comment again. Consider the following code: printf '%d\n' "$(($(sed -n '/...
0
votes
1answer
864 views

Arithmetic syntax error with a number variable

I am having a difficulty with an arithmetic syntax error. I am reading the name of text files from the command line and count the number of the lines of each file. NUM=$(wc -l "$text") and then I ...
0
votes
1answer
118 views

Convert a column of epochs into a time column starting from 0

What I have Hi, imagine a lot of this files, where first column is epoch, and the other(s) column(s) are some data: 1000333,34,1 1001456,56,0 1005356,34,2 What I need I need to transform them into ...
0
votes
1answer
67 views

awk: arithmetic does not work on a particular field

I have several lines such as: N2O + M = N2 + O + M 1.300E+11 0.00 59620.0 I need to substitute the third from last field with a factor of itself which does not work ...
4
votes
1answer
323 views

Shell Arithmetic Expansion with Quotes

In Bash and Dash, using quotes in an Arithmetic Expansion is illegal: $ bash -c 'x=123;echo $(("$x"))' bash: "123": syntax error: operand expected (error token is ""123"") $ dash -c 'x=123;echo $(("$...
3
votes
3answers
545 views

How to treat numbers starting with zeroes as decimal integer

Input would be file name like following : A-B-000001-C A-B-000002-C ..... ..... A-B-999999-C All file should be sequential. I want to find missing sequential file names. For this I'm separating 6 ...