1

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.

2 Answers 2

0

In bash you should be able to do this:

logger(){
  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:

logger(){
  tee >(command logger)
}

echolog(){
  printf '%s\n' "$@" | logger
}
0
0

I typically write a logging function; something like

mylog()
{
  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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .