5

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.

3
  • @spicavigo Tried that already with no results :/ Commented Nov 7, 2011 at 14:21
  • can you swap out some dbl-quotes for single quotes?, i.e. command="curl --data-urlencode 'param_value@/tmp/some folder/data.txt' www.someurl.com" ... Good luck! Commented Nov 14, 2011 at 4:59
  • I'm just having a dynamic path, which is having space in it, any remedy to fix this? Commented Sep 8, 2014 at 6:32

2 Answers 2

5

You should use eval :

eval "$command"
Sign up to request clarification or add additional context in comments.

1 Comment

No luck. The command does not work, but because it looks for curl in the current directory rather than in the bin dir. (says /home/Shtong/curl does not exist)
0

Have you tried:

command='curl --data-urlencode "param_value@/tmp/some\ folder/data.txt" www.someurl.com'

3 Comments

I did, but sorry, same results !
@Shtong: I forgot to remove the escaping of the second quotation mark. Changed it in my code now.
Oh, didn't see your mistake, but with or without mistake the error is the same :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.