3

I'm writing a bash script which has to pass a variable to another program:

./program $variable

The problem is, it is absolutely necessary for $variable to be passed as a single parameter, which isn't the case if it contains whitespace.

variable=Hello World
./program $variable
-> program receives two arguments: 'Hello' 'World'

Quoting it doesn't do anything at all (well done, bash devs):

variable=Hello World
./program "$variable"
-> program receives: 'Hello' 'World'

Double quoting it does crazy stuff (well done, bash devs):

variable=Hello World
./program "'$variable'"
-> program receives: "'Hello" "World'"

Is there an easy way to do this? Heck, is there a way to do this at all?

Update: Okay, since the problem doesn't seem to be bash, here's some additional info. The program I'm passing arguments to is a python script. Without modifying the arguments in any way, I get

print sys.argv
-> ['/usr/bin/program', "'Hello", "World'"]

How can I fix that?

Edit: No, I haven't tried

variable="Hello World"

because I never declare $variable. It's not being declared inside my bash function and I'm not allowed to modify it.

Edit: Okay, I got it to work that way.

local temp="$variable"
./program "$temp"

I'd like to know why it works that way and not any other way, though.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • 1
    the problem is in the "program" ; `./program "$variable"` passes one argument which can contain spaces, what the program is doing? – Nahuel Fouilleul Oct 22 '12 at 08:34
  • 2
    did u try with `variable="Hello World"` – Vijay Oct 22 '12 at 08:36
  • @sarathi: No, since I actually never declare $variable. I just pass it on. – Aran-Fey Oct 22 '12 at 08:41
  • Where is `variable` coming from? – ShankarG Oct 22 '12 at 08:55
  • How is `variable` declared, and where? The bug might be there. – cdarke Oct 22 '12 at 09:02
  • No idea, actually. The bash function is one of those that handle tab-complete (in /etc/bash_completion.d/myprogram). It's $COMP_WORDS, if you really need to know. – Aran-Fey Oct 22 '12 at 09:08
  • you don't need quotes in the assignment (as you've shown it in your latest update). E.g., this is perfectly fine: `temp=$variable`; no need for: `temp="$variable"`. However, you do need the quotes when passing the variable as an argument to `./program`, otherwise the value of the variable is expanded; e.g., do use: `./program "$temp"` and not `./program $temp`. You can reproduce this without even using the variable; eg: good: `./program "one two"`; bad: `./program one two` – michael Aug 12 '13 at 04:27

5 Answers5

1

did you try with var="hello world"?

i tried this in my solaris box.

> setenv var "hello world"
> cat temp.sh
#!/bin/sh

echo $1
echo $2
> ./temp.sh "$var"
hello world

>

as you can see the $2 is not printed.$var is considered as only one argument.

Vijay
  • 65,327
  • 90
  • 227
  • 319
1

When you call your script pass the arguments within quotes.

Example script:

#!/bin/bash
for arg in "$@"; do
 echo "arg: $1";
 shift;
done

When you call it with:

./program "parameter with multiple words" parameter2 parameter3 "another parameter"

The output should be:

arg: parameter with multiple words
arg: parameter2
arg: parameter3
arg: another parameter
morgents
  • 505
  • 5
  • 11
1

Have a look on http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html .

The problem is that the expansion of variables is done before of the command line parameters hence your behavior.

You might work it arround with setting IFS to something weird as

IFS='###' V='foo bar baz'; ./program $V 
Daniel Voina
  • 3,185
  • 1
  • 27
  • 32
  • Thanks for the link and explanation. Looking at that guide, it looks like I'll never get to like bash. – Aran-Fey Oct 22 '12 at 09:10
0

This is almost certainly a problem in the way you are reading the variable in your program.
For instance suppose this is your script (just one line for testing):

echo "$1"

Let's call it echo.sh. If you run echo.sh "test best", you will get test best.

But if your program says

echo $1

you might get behaviour like what you're seeing.

ShankarG
  • 1,105
  • 11
  • 26
0

The problem seems to be inside the "program"

variable="Hello World"    # quotes are needed because of the space
./program "$variable"     # here quotes again

and inside the program

echo "program received $# arguments:" 
i=1
for arg in "$@"      # again quotes needed
do echo "arg $((i=i+1)): '$arg'"    # and again
done
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36