How can I do division with variables in a Linux shell?
Those variables are shell variables. To expand them as parameters to another program (ie expr
), you need to use the $
prefix:
expr $x / $y
The reason it complained is because it thought you were trying to operate on alphabetic characters (ie non-integer)
If you are using the Bash shell, you can achieve the same result using expression syntax:
echo $((x / y))
Or:
z=$((x / y))
echo $z
Divide two variables in bash
shell parsing is useful only for integer division:
var1=8
var2=4
echo $((var1 / var2))
output:
2
instead your example:
var1=3
var2=4
echo $((var1 / var2))
ouput:
0
it's better to use bc:
echo "scale=2 ; $var1 / $var2" | bc
output:
.75
scale is the precision required
How do I divide the output of a command by two, and store the result into a bash variable?
middle=$((`wc -l < file` / 2))
How to divide with awk?
You can use this awk:
awk '$0+0 == $0 { printf "%.3f\n", $0 / .03 }' file
-31137.346
-31068.467
-31007.352
$0+0 == $0
will make sure to execute this division for lines with valid numbers only.printf "%.3f"
will print result with 3 precision points.
Best way to divide in bash using pipes?
Using bc
:
$ bc -l <<< "scale=2;$(find . -name '*.mp4' | wc -l)/3"
2.33
In contrast, the bash shell only performs integer arithmetic.
Awk is also very powerful:
$ find . -name '*.mp4' | wc -l | awk '{print $1/3}'
2.33333
You don't even need wc
if using awk
:
$ find . -name '*.mp4' | awk 'END {print NR/3}'
2.33333
division in shell script
Bash does integer
math, not floating point. You will need to use awk
or bc
to provide floating point output. e.g. with bc
scalingFactor=$(printf "scale=3; %d/%d\n" $normNum $mappedReads | bc)
In bash how do I divide two variables and output the answer rounded upto 5 decimal digits?
The problem here is that you missed the echo
(or printf
or any other thing) to provide the data to bc
:
$ echo "scale=5; 12/7" | bc
1.71428
Also, as noted by cnicutar in comments, you need to use $
to refer to the variables. sum
is a string, $sum
is the value of the variable sum
.
All together, your snippet should be like:
sum=12
n=7
output=$(echo "scale=5;$sum/$n" | bc)
echo "$output"
This returns 1.71428
.
Otherwise, with "scale=5;sum/n"|bc
you are just piping an assignment and makes bc
fail:
$ "scale=5;sum/n"|bc
bash: scale=5;sum/n: No such file or directory
You then say that you want to have the result rounded, which does not happen right now:
$ sum=3345699
$ n=1000000
$ echo "scale=5;($sum/$n)" | bc
3.34569
This needs a different approach, since bc
does not round. You can use printf
together with %.Xf
to round to X
decimal numbers, which does:
$ printf "%.5f" "$(echo "scale=10;$sum/$n" | bc)"
3.34570
See I give it a big scale, so that then printf
has decimals numbers enough to round properly.
How to divide a column to a number in bash shell if the other column had a condition true?
Try this:
awk '$1 =="G" {$8 = $7 / 255} 1' file > new_file
How to divide columns by a constant in bash
In awk:
$ awk '{for(i=2;i<=NF;i++)$i/=3}1' file
ERR843978.746 5 18 23.6667
ERR843978.1705 2.33333 15 17
ERR843978.5030 1 15.3333 19.3333
ERR843978.8855 5 16 17.3333
ERR843978.4162 5.33333 16 21.3333
ERR843978.421 4.66667 17 18
ERR843978.4599 4 20 15.3333
Pipe it to column
for nices output:
$ awk ... | column -t
ERR843978.746 5 18 23.6667
ERR843978.1705 2.33333 15 17
ERR843978.5030 1 15.3333 19.3333
Related Topics
Command Not Found in Bash's If-Else Condition When Using [! -D "$Dir"]
Linux: Modpost Does Not Build Anything
Is There a Special Restriction on Commands Executed by Cron
Where Does Pp (Par) Unpack Add (-A) Files
New Scala Worksheets Not Evaluated in Eclipse
How to Get Details of All Modules/Drivers That Were Initialized/Probed During the Linux Kernel Boot
How to Do 'Ret' Instruction from Code at _Start in MACos? Linux
How to Run a Shell Script by Cron Job
Bash Script to Find and Display Oldest File
Error While Loading Shared Libraries: Libncurses.So.5:
Is There a Good Way to Detect a Stale Nfs Mount
Getting Message "Sudo: Must Be Setuid Root", But Sudo Is Already Owned by Root
Bash: Let Statement VS Assignment
Append Text to File from Command Line Without Using Io Redirection