Linux Lite Forums

Software - Support => Other => Topic started by: Vera on July 27, 2018, 07:14:19 PM

Title: How to get absolute value of integer? abs command does not work, man pages exist
Post by: Vera on July 27, 2018, 07:14:19 PM
I'm trying to get the absolute value of an integer on my LL 4.0 machine. I'd like to use it in the command line. I looked up the man pages for the abs command, and it comes up there. But when I try to use the abs command, it says:
Command 'abs' not found, did you mean...
Then it lists some other things.

I'm puzzled why abs comes up in man pages but it won't work on command-line.

Anyway, the problem is, I can't find how to actually get the absolute value of an integer.  Is there a different method I should use?
Title: Re: How to get absolute value of integer? abs command does not work, man pages exist
Post by: kpanic on July 28, 2018, 12:06:14 PM
Just remove the minus sign if it's there, like this:

num=-4
num=$(echo $num |sed 's/\-//g')

echo $num
4
Title: Re: How to get absolute value of integer? abs command does not work, man pages exist
Post by: Vera on July 28, 2018, 01:32:41 PM
Thanks @kpanic , your solution worked well. Last night I did a clunkier workaround, where I tested if the number was less than zero, and if so, I subtracted the number from zero. But your solution with sed looks like it's a lot faster. It worked beautifully when I tested it today. Marking as solved.
Title: Re: How to get absolute value of integer? abs command does not work, man pages exist
Post by: kpanic on July 28, 2018, 01:47:38 PM
Glad to hear that @Vera .

Yes, there are many ways to do this. Happy to help :)