Question

i want to put some arguments into a variable f.e.

CFLAGS=-c --sysroot=$sysroot

but bash complains that it does not find the file --sysroot... why? How can I put this arguments into the variable and pass them later to the program.

Additionally i would like to do something like:

for dir in ${include_dirs[*]};
do
    CFLAGS=$CFLAGS "-I$dir"
done

but this does also not work as expected.

EDIT: One solution

CFLAGS=("-c" "--sysroot=$sysroot")

and in the loop

CFLAGS=("${CFLAGS[0]}" "-I$dir")

i am wondering if there is maybe a more obvious solution.

Was it helpful?

Solution

In shell quotes are pretty important so:

CFLAGS="-c --sysroot=$sysroot"

otherwise BASH interprets it as 2 different argument.

Alternatively you can use arrays to store it:

CFLAGS=("-c" "--sysroot=$sysroot")

And use them later as:

"${CFLAGS[@]}"

very important to quote the array expansion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top