Search code examples
bashquotesdouble-quotes

bash: quote var with quotes


I try to make a dummy script for debugging. The script should pass the command and all params to syslog. But when I get a param which has quotes I only get a failure.

#!/bin/sh

set -x
param="$*"

logger -t $0 -p local3.debug "$*"
#logger -t $0 -p local3.debug "$param"
#logger -t $0 -p local3.debug "$(echo "$param")"
#logger -t $0 -p local3.debug "${param//\"/}"
#logger -t $0 -p local3.debug "${param[@]}"

I tryd some variants I found, but nothing works.

# d-iptables -L dkdsdds "ddd"
+ param='-L dkdsdds ddd'
++ echo '-L dkdsdds ddd'
+ logger -t /usr/sbin/d-iptables -p local3.debug '-L dkdsdds ddd'
logger: invalid option -- L

Usage:
 logger [options] [message]

Options:
 -d, --udp             use UDP (TCP is default)
 -i, --id              log the process ID too
 -f, --file <file>     log the contents of this file
 -h, --help            display this help text and exit
 -n, --server <name>   write to this remote syslog server
 -P, --port <number>   use this UDP port
 -p, --priority <prio> mark given message with this priority
 -s, --stderr          output message to standard error as well
 -t, --tag <tag>       mark every line with this tag
 -u, --socket <socket> write to this Unix socket
 -V, --version         output version information and exit

Solution

  • I would suggest you change your script to #!/bin/bash and use

    logger -t $0 -p local3.debug -- "$(printf "%q " "$@")"
    

    You need the -- so logger doesn't think the -L is one of its options.