Absolute value of a number

后端 未结 4 1900
梦毁少年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:20

    You might just take ${var#-}.

    ${var#Pattern} Remove from $var the shortest part of $Pattern that matches the front end of $var. tdlp


    Example:

    s2=5; s1=4
    s3=$((s1-s2))
    
    echo $s3
    -1
    
    echo ${s3#-}
    1
    

提交回复
热议问题