# Tagged Questions

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.

74k views

### Is there a way to get the min, max, median, and average of a list of numbers in a single command?

I have a list of numbers in a file, one per line. How can I get the minimum, maximum, median and average values? I want to use the results in a bash script. Although my immediate situation is for ...
36k views

### Parenthesis in bash 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?
25k views

### 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 ...
17k views

### 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 ] ...
2k views

### 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....
29k views

### How to compare to 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
29k views

### 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 ...
7k views

### how to suppress bash octal number interpretation? (to be interpreted as decimal)

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 ...
26k views

### 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?
14k views

### 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 ...
2k views

### Why does “\$(( ~33 ))” produce -34?

\$ echo \$(( 255 )) 255 \$ echo \$(( 33 )) 33 \$ echo \$(( ~33 )) -34 \$ echo \$(( ~255 )) -256 \$ and my kernel is: \$ uname -a Linux HOSTNAME 3.2.0-40-generic-pae #64-Ubuntu SMP Mon Mar 25 21:44:41 UTC ...
23k views

### 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 : ...
1k views

### Why does a=0; let a++ return exit code 1?

Try it: \$ a=0 \$ let a++ \$ echo \$? 1 # Did the world just go mad? \$ echo \$a 1 # Yes, it did. \$ let a++ \$ echo \$? 0 # We have normality. \$ echo \$a 2 Contrast with this: \$ b=0 \$ let b+=1 \$ echo \$? 0 ...
284 views

### 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 ...
2k views

### 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 ...
10k views

### Bash: integer expression expected, using read/test

I'm learning the basics of shell programming, so I wrote something simple like #!/bin/bash read var1 read var2 if [ var1 -lt var2 ]; then echo "var1 is lt var2" else echo "var2 is lt var1" fi ...
900 views

### Using bash “double paren” arithmetic expansion, math fails with leading zero

I have a simple script that deals with hours and minutes. If I want to calculate number of minutes since midnight having a string s hh:mm I tried splitting string then doing hh * 60 + mm My problem ...
268 views

### Is it possible to use variables in count braces in Bash?

I'm searching for a solution to a Bash 4 problem I have. Bash 4 is able to iterate with leading zeros. For the line: for i in {001..005}; do echo -n \$i" ";done;echo the output is 001 002 003 004 ...
504 views

### Trying to do arithmetic in Perl rename tool (Debian)

Let's suppose I have five mp3 files: 01-trackfoo.mp3 02-trackbar.mp3 03-trackbaz.mp3 04-trackabc.mp3 05-trackxyz.mp3 Now I give a quick listen to the files (just mplayer on console will do the ...
780 views

### What is the rationale for the bash shell not warning you of arithmetic overflow etc.?

There are limits set for the arithmetic evaluation capabilities of the bash shell. The manual is succinct about this aspect of shell arithmetic but states: Evaluation is done in fixed-width ...
898 views

### Emacs simple arithmetics in query replace

Is it possible to do the following in emacs: Query replace all numbers in a region by the number +n, for example if you have: 12 and 7 are nice numbers let n be 3, then it should transform to 15 ...
5k views

### In bash, how to convert 8 bytes to an unsigned int (64bit LE)?

How can I 'read/interpret' 8 bytes as an unsigned int (Little Endian)? Perhaps there is a Bash-fu magic conversion for this? UPDATE: It seems that something got cross-wired in the interpretation of ...
20k views

### Calculate variable, and output it to another variable

The only calculator I know is bc. I want to add 1 to a variable, and output to another variable. I got the nextnum variable from counting string in a file: nextnum=`grep -o stringtocount file.tpl....
1k views

### How to iterate a zero padded integer in bash?

How does one iterate a string with the form "[A-Z][0-9]*" Or for example: "A000001"? After receiving the variable I split: current_=\$(mysql -h"\$mysqlhost" -u"\$mysqluser" -p"\$PASS" "\$DBNAME" -se "...
3k views

### How do I create a bash script that sums any number of command line arguments?

How would I create a bash script that the user can use to sum any amount of command line arguments? For example, say my script is called sum: sum 3 3 sum 3 5 8 sum 9 8 21 38 And so on.. I ...
46k views

### 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+\$...
1k views

### How to sum time using bash?

I want to know the total amount of time that a series of processes would take in my computer to decide if I should running there or in a stronger computer. So, i am forecasting the running time of ...
6k views

### Bash's conditional operator and assignment

Can we use bash's conditional operator with assignment operators after colon? Bash reference manual explains the arithmetic operators as follows. conditional operator expr ? expr : expr assignment =...
4k views

### awk high precision arithmetic

I am looking for a way to tell awk to do high-precision arithmetic in a substitution operation. This involves, reading a field from a file and substituting it with a 1% increment on that value. ...
409 views

### Arithmetic operations with expr and variables

Why won't this statement work? total=`expr \$mPercent / 100 "*" .482 + \$fPercent / 100 "*" .518` mPercent is a number as a result of an awk statement, as well as fPercent. I also get "expr: non-...
441 views

### Redirect stderr of the double-parentheses construct

I'd like to be able to redirect the stderr of a double-parentheses construct. For example: a=\$((\$var/\$var2)) would output some error messages if \$var2 = 0, I do not want the user to see this. I ...
168 views

### How do I convert a string into a number interpreted in certain base in bash script?

I'm simply trying to convert a string \$RECORD_HDR to a number \$RECORD_SIZE, where echo \$RECORD_HDR gives 003D and echo \$RECORD_SIZE should give 61
450 views

### Why does \$((40-35)) turn into 5?

What kind of strange shell syntax is hidden behind \$((40-35)) and how come it calculates a value? Example: \$> echo \$((40-35)) 5
346 views

### Get pairs of lines and multiply [closed]

I'm trying to parse an html file for some values using bash. So I've got a big text file with numbers like this 3 2 4 2 What I want to do is get two lines, multiply both numbers and then store the ...
5k views

### Increment number in bash variable string

VERSION="4.0beta12" NEXT_VERSION=????? Writing an automatic release script, I have the current version in a variable \$VERSION, and I would like to calculate the next version as \$NEXT_VERSION. Rule: ...
7k views

### Calculate and divide by total with AWK

Given the following data file... foo 10 bar 20 oof 50 rab 20 ... how would I print column two as a percent of the total of column two? In other words, I want... foo 10 10% ...
5k views

### Comparison of decimal numbers in bash

My search this morning was about how could I compare two decimal numbers in bash, and I came to this answser: How to compare to floating point number in a shell script. This one, however, doesn't ...
578 views

### Unit multiplier conversion in a shell script

I need to convert human-readable sizes into bytes. Unfortunately numfmt function is not accessible for me. Is there any other alternative? I need a shell/bash function to call from inside the sh ...
50 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 ...
4k views

### Remove scientific notation bash script

When I convert MB to GB I'm getting below output. I want this output in normal format. When I use bc I'm getting an error. The text file contains nearly 100 such lines like this. I want to print this ...
129 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?
522 views

### Subtract floating numbers in shell script

I'm trying to do some calculation in shell script with CPU usage. Which return floating point number. But when I subtract this number I'm getting error. See the following code and error. Code #!/bin/...
159 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 ...
7k views

### case + how to implement equal or less or greater in case syntax

My target is to verify a range of number with (only with case + esac), and print the range. So for example: If the number is between 0 and 80, print >=0<=80 If the number is between 81 and 100 ...
318 views

### Generate lists of page numbers for 2-up duplex printing: 2,3,… and 1,4,…

How can I generate the following increments? 2,3,6,7,10,11,14,15,18,19,22,23,26,27,30,31,34,35,38,39,42,43,46,47,...,135 and 4,5,8,9,12,13,16,17,20,21,24,25,28,29,32,33,36,37,40,41,44,45,48,49,...,...
16k views

### How to calculate values in a shell script?

I run this command in the terminal: grep "bla bla blah" blah* | echo "Blah: \$(wc -l) / \$(ls | wc -l) * 100" And I get this output: Blah: 44 / 89 * 100 What I expect to see: 49.4 Is there a ...
2k views

### Error when subtracting two negative numbers in ksh: “assignment requires lvalue”

I am trying to debug someone else's script: The code line is: y=\$((\${oldvalue[\$x]}-\${newvalue[\$x]})) y gets calculated fine as long as both sides are positive numbers. However, I have a ...
57 views

### Moving sub-directory to new parent directory where the new directory name is incremented by 1

I am attempting to move a sub-directory from one parent directory to another for hundreds of instances, while changing the name of the sub-directory during the move. My directories are a set of ...