0

I'm trying to dynamically compute arguments to call m4 with. But I can't get strings in quotes to work properly.

Im trying to call m4 -DFOO="foo oh oh" sample.m4 with the following code:

test.sh:

#!/bin/bash
args=(-DFOO="foo oh oh")
m4 ${args[@]} sample.m4

sample.m4:

foo is FOO
bar is BAR

When I run the m4 command manually it works fine. When i use my test script, I get the following error :

m4: cannot open `oh': No such file or directory
m4: cannot open `oh': No such file or directory
foo is foo
bar is BAR

Clearly, it's trying to open the words in the string as files. If I escape the quotes, I get this error :

m4: cannot open `oh': No such file or directory
m4: cannot open `oh"': No such file or directory
foo is "foo
bar is BAR

How do I get this to work properly?

ikkentim
  • 1,639
  • 15
  • 30

1 Answers1

3

Always quote your variable/array expansions unless you have a reason not to (which is never the case mostly)

m4 "${args[@]}" sample.m4

An unquoted expansion has caused the words in the array to be split and ended up getting unequal number of parameters to m4 command.

Inian
  • 80,270
  • 14
  • 142
  • 161