user1082754
user1082754

Reputation:

bash: using grep in if statement, prevent echo

I have this bash script:

#!/bin/bash

external_output="Oliver's AirPort Express"

if ~/.bin/audiodevice | grep "$external_output"
then
  ~/.bin/audiodevice output "Internal Speakers"
  echo "Internal Speakers"
else
  ~/.bin/audiodevice output "$external_output"
  echo "Oliver's AirPort Express"
fi

If the grep is matched, then it of course echoes the match. As I am using it in an if statement, I don't want this to echo.

How can I use grep in my if statement without having it announce the result to me when I run the script?

Upvotes: 1

Views: 6461

Answers (1)

Chris Seymour
Chris Seymour

Reputation: 85875

You want grep -q "$external_output" to suppress the output. From man grep:

-q, --quiet, --silent

Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected (-q is specified by POSIX).

Upvotes: 5

Related Questions