Monday, November 24, 2008

Bash: Passing Arguments with Quotes

Today I needed to write a bash script that eventually calls another program, passing all the arguments to it. It seemed like a simple enough task: just use $@. However, the problem was that the program accepts arguments surrounded with quotes, and the $@ stripped the quotes away. I was really surprised that google didn't immediately find an answer.

Finally I reached this forum:
http://www.linuxforums.org/forum/linux-programming-scripting/62564-bash-script-problem-preserving-quotes-arguments.html

And to save you the trouble, here's a script that preserves the quotes:
#!/bin/bash

# blah blah blah

./foo "$@"


For more on posts, see here.

7 comments:

  1. thank you. you address me to fix that problem.

    ReplyDelete
  2. Because of your post, it now popped up in google quite soon, thanks!

    ReplyDelete
  3. Wow, it took me a full day to figure out what was wrong with my script, and I could finally fix it with your help. Thanks.

    ReplyDelete
  4. A bash function that returns each paramenter (whether it contains spaces and quotes or not) in its own line, like $1 \n $2 \n $3 .... surrounded by quotes. With that you can solve everything

    ReplyDelete