start-stop-daemon quoted arguments misinterpreted start-stop-daemon quoted arguments misinterpreted shell shell

start-stop-daemon quoted arguments misinterpreted


Try

DAEMON_OPTS="-la '/folder with space/'"start-stop-daemon --start ... -- $DAEMON_OPTS

What happens is that the outer quotes of DAEMON_OPTS are stripped but the inner (single quotes) remain. So the next line will read:

start-stop-daemon --start ... -- -la '/folder with space/'

which is what you want.

It is also possible to achieve the same effect with escaping but you need a lot of escapes for this: First, to protect the quotes during the assignment, then later when the start line is parsed and variables are expanded and maybe even once more or less. :) bash -x is your friend for things like that.

[EDIT] The code above does work with Bourne and Korn shell on anything but Linux. On Linux, with ksh or bash, the shell will add additional quotes which mess up the whole thing:

FOLDER="/folder with space/"DAEMON_OPTS="-la $FOLDER"start-stop-daemon --start ... -- $DAEMON_OPTS

If you run it with -x, you'll see:

FOLDER='/folder with space/'DAEMON_OPTS='-la ~/folder with space/'ls -la '~/folder' with space/

So only the first word gets protection (probably because it contains a special character). If I add single quotes around $FOLDER, I get:

FOLDER='/folder with space/'DAEMON_OPTS='-la '\''~/folder with space/'\'''ls -la ''\''~/folder' with 'space/'\'''

Well done. Workaround: Split the options into two variables: One with the options and the other with the path:

start-stop-daemon --start ... -- $DAEMON_OPTS "$DAEMON_PATH"

[EDIT2] This works, too:

FOLDER="$HOME/folder with space/"opt[0]=-laopt[1]=$FOLDERls "${opt[@]}"

i.e. put the words into an array.


I thought I'd post the final quotations used in my working init script:

COMMAND="/path/to/script -opt param param2 param3"DAEMON_OPTS=" 0.0.0.0:$PORT -dest $OUTPUT_DIRECTORY -command"start-stop-daemon --start --background --make-pidfile --pidfile $PID --exec $DAEMON -- $DAEMON_OPTS "\"$COMMAND\""

Obviously an incomplete and non-functional example but I hope you get the gist of it. The double quotations with the inner pair escaped is what did the trick.


Whenever you have a bash variable with spaces, make sure you use quotes when referring to it.

start-stop-daemon --start --make-pidfile --pidfile $PID --exec $DAEMON -- "$DAEMON_OPTS"