staticx
staticx

Reputation: 1281

pass bash variable as argument to gcc

I aim to pass some variable values to gcc. Here my example:

command:

gcc -Q -fvpt -fwrapv -fwhole-program --help=optimizers 

output:

-fvpt                       [enabled] 
-fwhole-program             [enabled] 
-fwrapv                     [enabled] 

and When I run:

var="-fvpt -fwrapv -fwhole-program"; gcc -Q $(var) --help=optimizers 

output:

-fvpt                       [disabled] 
-fwhole-program             [disabled] 
-fwrapv                     [disabled] 

Why it does not work ?

Upvotes: 2

Views: 1010

Answers (3)

chepner
chepner

Reputation: 531878

You should use an array, not a flat string, to store multiple options. (For the example, a string works, but it will fail for arguments that themselves contain whitespace, and may fail for options containing pattern metacharacters.)

args=(-fvpt -fwrapv -fwhole-program)
gcc -Q "${args[@]}" --help=optimizers 

Upvotes: 1

grimsock
grimsock

Reputation: 794

var="-fvpt -fwrapv -fwhole-program" && gcc -Q $var --help=optimizers Should work for you.

Upvotes: 1

Dark Falcon
Dark Falcon

Reputation: 44191

$(var) attempts to execute the command var and use its output in the command line. To expand a variable in bash, you would use $var

var="-fvpt -fwrapv -fwhole-program"; gcc -Q $var --help=optimizers 

Upvotes: 2

Related Questions