SnakeDoc
SnakeDoc

Reputation: 14431

Bash imported variables break configure script when double-quoted

I have a bash script which imports same variables from another static file, which itself uses environment variables set by another file when the script is invoked.

This is the file that gets imported and sets some variables.

# package.mk
PKG_NAME="binutils"
PKG_VERSION="2.24"
PKG_URL_TYPE="http"
PKG_URL="http://ftp.gnu.org/gnu/binutils/${PKG_NAME}-${PKG_VERSION}.tar.bz2"
PKG_DEPENDS=""
PKG_SECTION="devel"

PKG_CONFIGURE_OPTS="--prefix=${TOOLS} \
                    --target=${TARGET} \
                    --with-sysroot=${TOOLS}/${TARGET} \
                    --disable-nls \
                    --disable-multilib"

It is used by the builds script as so:

#!/bin/bash

# Binutils

. settings/config

pkg_dir="$(locate_package 'binutils')"

. "${pkg_dir}/package.mk"

# etc...

"${CLFS_SOURCES}/${PKG_NAME}-${PKG_VERSION}/configure" "${PKG_CONFIGURE_OPTS}"

# etc...

This script first imports the settings/config file which has a bunch of global variables used by this script and others, and exports them so they are available as environment variables. It then locates the correct package.mk file for the specific component we are building, and imports it as well. So far, so good.

However when I double-quote the options (PKG_CONFIGURE_OPTS) for the configure script:

"${CLFS_SOURCES}/${PKG_NAME}-${PKG_VERSION}/configure" "${PKG_CONFIGURE_OPTS}"`

I get the following error:

gcc: error: unrecognized command line option ‘--with-sysroot=/root/LiLi/target/cross-tools/arm-linux-musleabihf’

If I leave it not quoted like:

"${CLFS_SOURCES}/${PKG_NAME}-${PKG_VERSION}/configure" ${PKG_CONFIGURE_OPTS}`

it works fine (--with-sysroot= is indeed a valid configure flag for binutils).

Why is this? What can I change so that I can double-quote that portion (going by the bash wisdom that one should double-quote just about everything).

Upvotes: 1

Views: 60

Answers (1)

John Kugelman
John Kugelman

Reputation: 361957

Quoting the variable means the entire thing is passed as a single argument, spaces and newlines included. You want word splitting to be performed so that the string is treated as multiple arguments. That's why leaving it unquoted works.

If you're looking for the "right" way to handle this, I recommend using an array. An array can hold multiple values while also preserving whitespace properly.

PKG_CONFIGURE_OPTS=(--prefix="$TOOLS"
                    --target="$TARGET"
                    --with-sysroot="$TOOLS/$TARGET"
                    --disable-nls
                    --disable-multilib)

...

"$CLFS_SOURCES/$PKG_NAME-$PKG_VERSION/configure" "${PKG_CONFIGURE_OPTS[@]}"

Upvotes: 4

Related Questions