Droopycom
Droopycom

Reputation: 1921

Shell variable expansion: Storing multiple command arguments that include spaces in one variable

I'm writing a shell script to drive xcodebuild, the Xcode command line frontend.

xcodebuild takes build settings on the command line in the following manner:

$ xcodebuild a=value b=value

value may contain spaces, for example it could be a list of paths.

So I would invoke it like so:

$ xcodebuild "a=value1 value2 value3" "b=value4 value5"

xcodebuild will report the settings as expected:

Build settings from command line:
    a = value1 value2 value3
    b = value4 value5

However when invoked as follow:

$ xcodebuild "a=value1 b=value2"

This will be interpreted as a single argument, assigning a value to build setting a:

Build settings from command line:
    a = value1 b=value2

Now, the real question is I want to write a shell script that will have all the build settings in one variable, e.g. I can do this:

SETTING_A="a=value1 value2 value3"
SETTING_B="b=value4 value5"

xcodebuild "${SETTING_A}" "${SETTING_B}"

However that does not scale.

And I want to do something like this:

SETTING_A="a=value1 value2 value3"
SETTING_B="b=value4 value5"
SETTINGS=${SETTING_A} ${SETTING_B}

xcodebuild ${SETTINGS}

However the above doesn't work and no matter what variations I tried, I can't keep SETTING_A and SETTING_B as two separate words/arguments for the command. It's either 1 if I use "$SETTINGS" or 5 if I use just $SETTINGS.

I'm looking for standard unix shell solutions but will entertain Bash or Zsh specific one if this is not doable in standard sh.

Upvotes: 0

Views: 320

Answers (1)

Inian
Inian

Reputation: 85710

Using standard sh (POSIX), just use set on the individual setting expansions and call "$@" to pass them, i.e.

SETTING_A="a=value1 value2 value3"
SETTING_B="b=value4 value5"

set -- "${SETTING_A}" "${SETTING_B}"
xcodebuild "$@"

Upvotes: 1

Related Questions