Print on stdout and log with logger

I have a bash script with several "echo STRING" commands. I would like to log STRING with logger but still print it on stdout. echo STRING | logger obliviously does not do it.

EDIT: echo STRING | tee >(logger) does it. >(COMMAND) is a process substitution, it substitutes the input to the process.

Asked By: mifrey


I typically write a logging function; something like

  echo "$@"
  echo "$@" | logger

mylog "Hello, world!"

It gives flexibility on what you want to log, and can even let you do things like add a timestamp in front of lines if that’s what you want.

Answered By: Stephen Harris

In bash you should be able to do this:

  tee >(command logger)

Then pipe to logger as you did before.

If you wanted to implement this via a custom logging function, as suggested by Stephen Harris, this could be done like so:

  tee >(command logger)

  printf '%sn' "$@" | logger
Answered By: bxm
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.