Search code examples
bashloggingescaping

Escape message to logger


I have a bash script where I want to log arguments and outputs to the local syslog. For this I use logger.

logger -p local0.notice -t TagToUse "Message to log"

The thing is that I want to log the output of $@ which may contain -u which is an option of logger. If that occurs I always get an error message logger: socket <Value of $@>: No such file or directory which means that logger still tries to parse the logging message for options. Is there a way to escape the logging message or to mark the message as string to not parse anything in it?

Edit: Trying to log via STDIN results in the same error message

echo "$@" > logger -p local0.notice -t TagToUse

Solution

  • Use -- to signal that logger should stop looking for options among the remaining arguments.

    logger -p local0.notice -t TagToUse -- "$@"
    

    You might want to use "$*" instead of "$@" to provide a single argument containing the log message.