I need to write a script that generates and executes a command with a variable number of arguments containing file names. These file names may contain spaces, and everything should work fine or without spaces.
For example, this generated command might look like this:
curl --data-urlencode "js_code@/tmp/some folder/data.txt" http://www.someurl.com
If I use a hard-coded command and execute it, everything works fine, with spaces and without them. However, if I create the command text in a string variable and execute the contents of the string, it seems that the command does not include quotes, using only the first part of the file:
script (simplified, just imagine that the command line is created using complex rules):
#!/bin/sh
command="curl --data-urlencode \"param_value@/tmp/some folder/data.txt\" www.someurl.com"
$command
Results:
$ ./test.sh
Warning: Couldn't read data from file ""param_value@/tmp/some", this makes an
Warning: empty POST.
curl: (6) Couldn't resolve host 'folder'
, , , exec, .
. , Cygwin. .