How to Do Division with Variables in a Linux Shell

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

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.

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)

Division in bash script

I think the pipe is the problem try something like this:

PorcUsado=$(echo "scale=2;$UsadoMem/$TotalMem" | bc -l) 

i haven't tested it yet but you have to echo the string and pipe the result from echo to bc.

EDIT: Correcting the variable names

Bash how to store a division to a variable

There are two things you need to be aware of. The first is that bc uses standard input for expressions so you would need to actually pipe your expression through it, or use the <<< redirection operator, one of:

c=$(echo "($a - $b) / 52" | bc)
c=$(bc <<< "($a - $b) / 52")

The <<< method is specific to bash and ksh (an possibly others, but I'm not really au fait with them). The other method can be used in most shells.

Secondly, you should be careful when using big numbers for this since bc has the annoying habit of splitting them across lines:

pax$ x=999999999999999999999999999999999999999999999999999999999999999999999

pax$ echo "$x / 7" | bc
14285714285714285714285714285714285714285714285714285714285714285714\
2

In order to avoid this, you need to change the line length:

pax$ echo "$x / 7" | BC_LINE_LENGTH=0 bc
142857142857142857142857142857142857142857142857142857142857142857142

How to subtract and divide the values in bash script and also i would like to know in the form of percent

You need a few more brackets :)

echo $((((10000-96756))/120))


Related Topics



Leave a reply



Submit