Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I evaluate or calculate the return value of a command line? For exemple, I count the number of lines with a grep and I want to know if that value is above X. If so, I want to print the number to a file.

Or I want to substract the value of a grep count to another grep count...

How can I manipulate return values that way ?

share|improve this question
up vote 10 down vote accepted

I think you are mixing two things the return value typically indicates if a command was successful (return value 0) or not (anything else). You can get the return value of a command from the variable $?

grep -c returns the count to stdout, to capture the count you can use something like

variable=$(grep -c pattern filename)

Afterwords you can calculate/access the variable how ever you want. See How can I do command line integer & float calculations, in bash, or any language available? how to calculate stuff with the output.

share|improve this answer
Yeah that was what I meant. And your answer is perfect ! – Philippe Carriere Jul 4 '12 at 19:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.