If I run this script: (Note: edited to reflect answer 1)
#!/usr/bin/env zsh
setopt shwordsplit
if [[ -f $1 ]]
then
echo "will compile $1"
else
echo "ERROR: $1 NOT found"
exit 1
fi
gcc_options=" -c -std=c99 -Wall -Wextra "
echo "gcc_options: ${gcc_options}"
gcc " ${=gcc_options} " " ${=1} "
if [[ $? -eq 0 ]]
then
echo "gcc compile worked"
else
echo "error in gcc compile"
fi
gcc "${=gcc_options}" "${=1}"
if [[ $? -eq 0 ]]
then
echo "gcc compile worked"
else
echo "error in gcc compile"
fi
Both compiles fail.
Output is:
will compile my_file.c
gcc_options: -c -std=c99 -Wall -Wextra
gcc: error: : No such file or directory
gcc: error: : No such file or directory
gcc: error: my_file.c : No such file or directory
gcc: fatal error: no input files compilation terminated.
error in gcc compile
gcc: error: : No such file or directory
gcc: error: : No such file or directory
error in gcc compile
First change:
To:
If some of these variables contain multiple words, you may need to turn on
shwordsplit(see here), which can also be done on a per variable basis (${=VAR}).Update
I made a mistake, you definitely don’t want quotes on
$gcc_options, so it should probably be${=gcc_options}. If you need to quote$1depends if$1contains spaces. So this should do: