Absolute value of a number

后端 未结 4 1901
梦毁少年i
梦毁少年i 2021-01-17 08:41

I want to take the absolute of a number by the following code in bash:

#!/bin/bash
echo \"Enter the first file name: \"
read first

echo \"Enter the second f         


        
4条回答
  •  野的像风
    2021-01-17 09:13

    I know this thread is WAY old at this point, but I wanted to share a function I wrote that could help with this:

    abs() { 
        [[ $[ $@ ] -lt 0 ]] && echo "$[ ($@) * -1 ]" || echo "$[ $@ ]"
    }
    

    This will take any mathematical/numeric expression as an argument and return the absolute value. For instance: abs -4 => 4 or abs 5-8 => 3

提交回复
热议问题