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.
210 questions
12 votes
2 answers
1k views
Why arithmetic syntax error in bash causes exit from the function?
I was debugging one script, and discovered unexpected behavour of handling arithmetic syntax error. Normally, when error happens, script is just continuing execution. #!/bin/bash func2 () { echo &...
4 votes
1 answer
303 views
using the bc calculator with obase and ibase to convert integer representation
I am using the bc calculator with obase and ibase to convert integer representation. I'm not used to it, because very basic things can be done with $(( expresion )) in bash, and most things that can't ...
2 votes
1 answer
495 views
What's the difference between '$var' and 'var' in an arithmetic expansion?
Bash accepts both these syntax: FOO=$(($BAR + 42)) and FOO=$((BAR + 42)) Which one is correct / most portable / less prone to errors? Or are both equally valid?
-3 votes
1 answer
211 views
How to split a number into unequal parts in Bash
Hypothetic example: There is a number that I will split into x parts (smaller numbers, in a range you see in the example). I need a way, ideally a one liner for the Linux Bash, to split a large number ...
1 vote
5 answers
281 views
how to subtract a date in format: 'xyz/[int][int]/[int][int][int][int]' from the current date?
Scenario I have containers which have last deploy dates in the format: month/date/year or feb/11/2024. I cant change the way they output the last deploy date. I'd like to subtract the last deploy date,...
-1 votes
2 answers
173 views
Numeric if conditions that read a command in a bash script
I'm trying to write a command that looks at my battery state and suspends the laptop if the battery is low. I tried using something like this: #!/bin/bash if [ $(acpi -b | cut -c 25) < 11 ] then ...
0 votes
1 answer
68 views
Logging sum of Mem and Swap from free command output
In relation to this: https://unix.stackexchange.com/a/754252/582781 Solution 1: free -g -s2 | sed -u -n 's/^Mem:\s\+[0-9]\+\s\+\([0-9]\+\)\s.\+/\1/p' >> memory.log Is there a way to add Swap to ...
-4 votes
1 answer
133 views
How to protect variable name inside shell arithmetic?
Variables can be protected with curly braces. Mynewvar=1 echo $Mynewvar 1 echo ${Mynewvar} 1 However I can no longer protect them when inside shell arithmetic. Mynewvar=1 echo $((Mynewvar+9)) 10 echo ...
3 votes
4 answers
615 views
Apply arithmetic into piped command
So far I have this: sudo find /path/to/dir -type f | xargs -d "\n" sudo stat -c "%Y %n" | {arithmetic to check if %Y is between 1685518962 and 1685624474??} | {show ...
1 vote
1 answer
352 views
Arithmetic operation issue in bash script | invalid arithmetic operator
From a bash script I am downloading a file from a server using curl call. Now I want to check if the file is fully downloaded. For this I am comparing the size of downloaded file and Content-Length ...
-5 votes
1 answer
357 views
printing prime number in bash [closed]
hi i am new to programming and currnetly learing bash script. please help with this error " line 28: expected `)' " Given below is my program. read -p "enter the number:" a while [...
-1 votes
1 answer
2k views
What is an operand?
I just want someone to explain what an "operand" is because I just don't understand it. Here is the context I'm reading this in: In the section about Shell Arithmetic in the bash shell ...
9 votes
1 answer
11k views
How does arithmetic expansion work with variables without the $ (dollar) sign?
I think the line below should be written differently, and the command should bug out. $ echo $((x=2, y=3, x+y)) 5 I thought the way to write this was: $ echo $((x=2, y=3, $x+$y)) 5 Both work, which ...
2 votes
1 answer
159 views
Arithmetic operation in terminal from an output
I want to print the lines of updates via this command dnf check-update --refresh --q --downloadonly | wc -l However during the output there occurs a blank line which means the true update number is ...
0 votes
2 answers
108 views
Unable to put output of basic calculator in a variable, formatting bytes to MB in bash
I want to get my data usage from my wifi device to display on my top bar, I can get the data usage in bytes using curl like this $bytes=(curl -d "Page=GetWANInfo" -X POST http://jiofi.local....