Você pode usar tee
para enviar a saída do comando para grep
e seu terminal ( /dev/tty
):
# Note grep's stdout is redirected to /dev/null
# because we only care about its exit code
if echo foo | tee /dev/tty | grep foo > /dev/null; then
echo OK
fi
Da estrutura de diretórios e dispositivos do POSIX :
/dev/tty
In each process, a synonym for the controlling terminal associated with the process group of that process, if any. It is useful for programs or shell procedures that wish to be sure of writing messages to or reading data from the terminal no matter how output has been redirected. It can also be used for applications that demand the name of a file for output, when typed output is desired and it is tiresome to find out what terminal is currently in use.
Não ajudará se você quiser que a saída do comando seja redirecionada para a saída padrão do script, que pode não ser o terminal.
Você pode salvar a saída do comando para processamento adicional:
# Save output in a variable (or a regular file, a named pipe, etc.)
output="$(echo foo)"
# Dump output to script's stdout
cat <<< "${output}"
# Check if output matches some pattern
if grep foo <<< "${output}" > /dev/null; then
echo OK
fi