Search code examples
bashnginxautotoolsconfigure

autotools configure error when passing options using shell variable


I wish to call configure command (to compile nginx) from a bash script like this:

CONF_OPTS=' --with-cc-opt="-O2 -g"'
./configure ${CONF_OPTS}

but I got the following error:

./configure: error: invalid option "-g"

When I pass the options like:

./configure --with-cc-opt="-O2 -g"

I got no error.

To reproduce:

curl -O  http://nginx.org/download/nginx-1.14.2.tar.gz
tar xfz nginx-1.14.2.tar.gz
cd nginx-1.14.2

OPTS='--with-cc-opt="-O2 -g"'
./configure ${OPTS}

Results

./configure: error: invalid option "-g""

But:

./configure --with-cc-opt="-O2 -g"

it is ok

I think it is not nginx related, but I look to me as bash quote substitution issue.


Solution

  • It will work like this:

    $ CC_OPTS=--with-cc-opt='-O2 -g'
    $ ./configure "$CC_OPTS"
    

    so that the expansion of $CC_OPTS is passed as a single argument to ./configure.

    But if you wanted also to pass, maybe:

    --with-ld-opt='-Wl,-gc-sections -Wl,-Map=mapfile'
    

    through a variable, you would need:

    $ CC_OPTS=--with-cc-opt='-O2 -g'
    $ LD_OPTS=--with-ld-opt='-Wl,-gc-sections -Wl,-Map=mapfile'
    $ ./configure "$CC_OPTS" "$LD_OPTS"
    

    because you need to pass two arguments to ./configure, and:

    ./configure "$CC_OPTS $LD_OPTS"
    

    passes only one, and will fail.