0

I am trying to create a bash file which calls a program that expects a parameter to submitted between two double quotes.

Take a look at my file content.

#!/bin/bash
echo ./yowsup/yowsup-cli demos --s $1 \"$2\" -c yowsup/config &
./yowsup/yowsup-cli demos --s $1 \"$2\" -c yowsup/config &

The echo part prints exactly the command I need, but when I comes to execute it (next line), I am messing it up.

How can I get this fixed?

Thanks!

This is how I need to call it:

./yowsup/yowsup-cli demos --s 9999999999 "my parameter" -c yowsup/config

Echo result:

./yowsup/yowsup-cli demos --s 9999999999 "my parameter" -c yowsup/config

Call result:

demos: error: unrecognized arguments: parameter

Copying and pasting echo results:

INFO:yowsup.demos.sendclient.layer:Message sent
  • 1
    What's in $2? What happens if you just double quote both the variables? --s "$1" "$2" -c – choroba Mar 22 '15 at 12:35
  • See my comments – user101903 Mar 22 '15 at 12:52
4

You need to escape the quotes in the echo command to see them in the output, but you shouldn't escape them in the actual command:

#!/bin/bash
echo ./yowsup/yowsup-cli demos --s $1 \"$2\" -c yowsup/config &
./yowsup/yowsup-cli demos --s $1 "$2" -c yowsup/config &

The quotes are used in the actual command to prevent splitting of $2 - which contains multiple words.

  • 3
    @user1688175 by the way, if you do want to see what commands get executed, use set -x instead of complicated echo commands. – muru Mar 22 '15 at 13:11
-1

I (nearly) always quote everything to "echo", so I would do the above like:

#!/bin/bash
echo "./yowsup/yowsup-cli demos --s $1 \"$2\" -c yowsup/config &"
./yowsup/yowsup-cli demos --s $1 "$2" -c yowsup/config &

The difference with @muru solution is, that there the original echo command is run "in the background" - here it's echoing to the screen in the foreground, but run as a background command. (@muru's also doesn't show the ampersand at the end of the command.)

  • At first it looked like you just copy-pasted @muru's answer and that might be why you get the downvotes. so I (hopefully) made the "As it stands" text less ambigious. – Anthon Jan 30 '16 at 6:15
  • Thanks Anthon, your edit looks like a much better explanation. (An "echo" command without quotes always looks dangerous to me.) – wanpelaman Feb 1 '16 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy