Try:
eval set " $@ "
or (safer if it can start with shell options):
eval set -- " $@ "
After that you will be able to use " $@ " .
As with all eval s, this one has all sorts of dangers. :-)
Hazard example:
$ set '`ls`' $ eval set -- " $@ " $ echo $# 28 $ echo "$1" COPYRIGHT
Edit: here is a protect shell function and an example of its use. I'm not sure I'm defending myself from everything, but he gets two obvious cases:
#! /usr/bin/env bash protect() { local quoted quoted="${1//$/\\\$}" quoted="${quoted//\`/\\\`}" # use printf instead of echo in case $1 is (eg) -n printf %s "$quoted" } foo=expanded set -- '-e -n $foo `ls` "bar baz"' eval set -- " $@ " echo "without protect, \$# is $#:" for arg do echo "[$arg]"; done set -- '-e -n $foo `ls` "bar baz"' eval set -- $(protect " $@ ") echo "with protect, \$# is $#" for arg do echo "[$arg]"; done
When I ran it:
without protect, $# is 46: [-e] [-n] [expanded] [OUT] [a.py] [ast_ex.py] ---snipped remaining ls output for space reasons--- [bar baz] with protect, $# is 5 [-e] [-n] [$foo] [`ls`] [bar baz]
If other characters require quotation marks, obviously how to add them to protect .
torek source share