Say you're writing a shell script. It takes an arbitrary number of command line arguments and passes them to a command line utility. When calling that utility, each argument needs to be prefixed with a flag. What's the correct way to escape this in your script, so that the utility sees the exact same strings that the shell script does?
Concrete example: passing regular expressions to
grep. You run your wrapper script:
./findit.sh needle "old socks" "spare change"
And you expect
grep to be called with those arguments as match expressions:
grep haystack.txt -e needle -e "old socks" -e "spare change"
-e prefix that complicates things; if it wasn't necessary, you could just use
"$@". What does your script need to do to make sure that
grep gets its arguments properly escaped? Is there a portable way to do this?