I have 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 must work fine with or without spaces.
For example, this generated command may 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 it all runs fine, with and without spaces. If I create the command text in a string variable however, and execute the string contents, it seems the command does not take the quotes into account, using only the first part of the file :
The script (simplified, just imagine the command string is created using complex rules) :
#!/bin/sh
#prepare
command="curl --data-urlencode \"param_value@/tmp/some folder/data.txt\" www.someurl.com"
#execute
$command
The 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'
I tried different things, switching quotes style, using things like exec, but I could'nt get this to work.
Any help would be appreciated
Thanks
Note : I should add all this testing is done on Cygwin. It may be important regarding path syntax.
You should use eval :
eval "$command"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With