The problem is not with the contents of the variable (and this is why you can try with as many escape characters as you want), but with the fact that bash replaces $ARGS
with the variable contents literally, so the space within it will be an actual space in the command.
Just use mkdir "$ARGS"
instead (i.e., enclose the variable reference in "
s)
UPDATE: As discussed in the comments, in order to have the -p
and other options as not being part of the directory name, define 2 variables instead:
OPTS="-p -whatever"
and
ARGS="/tmp/pwet/foo bar"
then later issue mkdir like this:
mkdir $OPTS "$ARGS"
Alternativelly, if you want full control over this using escape characters plus the possibility of having multiple directories, some with spaces, some without, options, all mixed together you can put the entire command in a variable and execute it with bash -c.
Check the example below as a starting point you can play with:
MKDIRCMD="mkdir -p \"A B\" \"C D\""
echo $MKDIRCMD # Notice the \ characters are NOT part of the string
bash -c "$MKDIRCMD"
OPTS="-p -x -whatever"
andARGS="tmp/pwet/foo bar"
. Then usemkdir $OPTS "$ARGS"
. Otherwise, there would be no way the shell to guess what spaces you intend to use as separators what you intend as part of an argument. – Marcelo Nov 05 '14 at 10:46