Reputation: 87
Is there any bash/shell command to get absolute difference of two numbers? Eg.-
Absolute diff of -10 and -5 is 5
, -10 and 10 is 20
, 7 and 21 is 14
& 100 and -11 is 111
.
Or any workaroud to find it? Please help.
Upvotes: 1
Views: 10624
Reputation: 627
You can do that without any external command by using ${var#-}
.
${var#Pattern}
Remove from$var
the shortest part of$Pattern
that matches the front end of$var
. tdlp
Example:
s2=7; s1=8
s3=$((s1-s2))
echo $s3
-1
echo ${s3#-}
1
Upvotes: 0
Reputation: 2273
This works for non-integer numbers also using bc
.
function abs_diff {
if [ $(bc <<<"$1 >= $2") -eq 1 ]; then
diff="$(echo $1 - $2 | bc)"
else
diff="$(echo $2 - $1 | bc)"
fi
echo $diff
}
abs_diff 5.0 1.0 #gives output: 4.0
abs_diff 1.0 5.0 #gives output: 4.0
Upvotes: 0
Reputation: 25419
You can do simple integer arithmetic directly in the shell using the $((...))
syntax.
function abs_diff {
local diff
diff=$(($1 - $2));
if [ $diff -lt 0 ]
then
diff=$((-$diff))
fi
echo $diff
}
It can be written more tersely as a single expression using the ternary ?
operator.
function abs_diff {
echo $(($1 >= $2 ? $1 - $2 : $2 - $1))
}
Then simply use
abs_diff -10 -5
in your code.
Update: “It would be great if you could explain the logic.” — There we go…
The basic idea is that we write a Bash function that takes two integers as arguments and returns the absolute difference of them.
A Bash function can be called like an external program using the FUNCTION_NAME [ARG...]
syntax. Inside a function, we can refer to its arguments via $1
, $2
, … just as we refer to a shell script's arguments outside of any function. To “return” a value from a function, we print it to standard output. (Don't abuse the return
statement for that. It is intended to report success or failure, not business data.) If we want to assign the result of a function call to a variable, we can use the VAR=$(COMMAND [ARG...])
syntax. A function definition has the syntax function FUNCTION_NAME { FUNCTION_BODY }
. If we declare variables that should be local to a function, we can use the local
keyword. This is a Bash feature.
Now let's see how we can compute the absolute value of the difference. We only have integer arithmetic so how can we do it? Obviously, if we subtract an integer n from an integer m, there are only two possible outcomes: a non-negative or a negative result. In the first case, we are done. In the second, all we need to do is to take the negative.
The first function does exactly this.
function abs_diff { # Define the function 'abs_diff'
local diff # with 'diff' as a local variable
diff=$(($1 - $2)); # to compute the difference of its first two arguments
if [ $diff -lt 0 ] # and if it is negative
then # then
diff=$((-$diff)) # negate the result
fi # and
echo $diff # finally print the result.
}
The second version is more terse. If you know the ternary ?
operator for example from C or Java, then it will come at no surprise. What this line
echo $(($1 >= $2 ? $1 - $2 : $2 - $1))
means is: If $1 >= $2
evaluates to true, then print $1 - $2
, otherwise $2 - $1
, which ensures that the result will always be non-negative.
If your need is a one-time thing and defining a function seems like overkill to you, simply copying the body of the second function to the place where it is needed might be a viable alternative.
Upvotes: 4
Reputation: 4837
Here is the simple one:
echo $(($1-$2)) | sed 's/-//'
./script.sh -10 -5
output
5
The logic is pretty simple. You provide parameters $1
and $2
to your script like
./script.sh -10 -5
and then output result of $1 - $2
using echo
. sed
will delete -
if it is presented.
Upvotes: 6
Reputation: 1822
How about this bash implementation:
export A=5
export B=10
echo "$A-$B" | bc | tr -d -
5
Upvotes: 2
Reputation: 239483
You can execute Python commands, from the command line with -c
parameter. So, you can make use of it, like this
python -c "import sys; print(abs(int(sys.argv[1]) - int(sys.argv[2])))" 100 -11
# 111
This script gets two arguments from the command line, converts both of them to integers, subtracts second value from the first value and prints the absolute value of the result
Upvotes: 2