Here the Bash uotd:
Say you've got a list of items separated by new lines, and you'd like to concatenate them in a string, but by ensuring you enclose each item in single quotes so as to enclose spaces and weird characters safely.
You do the following:
resulting_string=
for i in $list_of_things; do
resulting_string="${resulting_string}'${i}' "
done
# All good here, look at the contents.
# should be giving you what you expect
echo "$resulting_string"
Now try supplying that to a command:
some_command $resulting_string
# OOPS, some_command has an argv that would look like this (notice how it
# actually receives the single quotes with the arguments):
argv[0]=some_command
argv[1]='blah'
argv[2]='blih'
How do you fix that? Obvious, isn't it: you either eval or enclose the whole thing in another shell!
sh -c "some_command $resulting_string"
I really don't like shell scripting...
Add a comment