One liner to see if grep returned something?

Just tack the exit status check after grep, it will always get the exit status from the last command of the pipeline by default:

sudo dmidecode | grep -q ThinkPad; echo $?

Use -q to suppress any output from grep as we are interested in exit status only.


You can use command grouping if you fancy, but this is somewhat redundant here:

sudo dmidecode | { grep -q ThinkPad; echo $? ;}

If you're going to use this an shell script with an if check, just use -q as heemayl suggested:

if sudo dmidecode | grep -q Thinkpad
then
    echo "I'm a Thinkpad"
fi

Since the if block checks the command's exit status, we can rely on grep's exit status directly instead of printing $? and the comparing it to something else.


Inspired by Heemayl's answer:

sudo dmidecode | grep -q ThinkPad && echo true || echo false

This will return true if ThinkPad is found by grep and false if not.

Tags:

Grep

Xargs