英文:
bash variable containing spaces expanded in single quotes
问题
I have a script which calls make like this (simplified)
#!/bin/bash
if [[ -f one.source ]]
then
make one CPPFLAGS='-DONE -DTWO -DTHREE'
fi
if [[ -f two.source ]]
then
make two CPPFLAGS='-DONE -DTWO -DTHREE'
fi
The list of defines is getting long, and I'd like to avoid having to keep the two command lines in sync. I'd like to define a variable and use it in both commands. I tried
DEFINES='-DONE -DTWO -DTHREE'
DD="CPPFLAGS='"
DD+=$DEFINES
DD+="'"
With this,
echo $DD
CPPFLAGS='-DONE -DTWO -DTHREE'
seems to do what I want, but
make one $DD
fails with
make one 'CPPFLAGS='\''-DONE -DTWO -DTHREE'\'''
make: invalid option -- 'D'
make: invalid option -- 'T'
make: invalid option -- 'D'
make: invalid option -- 'T'
make: invalid option -- 'H'
Usage: make [options] [target] ...
<details>
<summary>英文:</summary>
I have a script which calls make like this (simplified)
#!/bin/bash
if [[ -f one.source ]]
then
make one CPPFLAGS='-DONE -DTWO -DTHREE'
fi
if [[ -f two.source ]]
then
make two CPPFLAGS='-DONE -DTWO -DTHREE'
fi
The list of defines is getting long, and I'd like to avoid having to keep the two command lines in sync. I'd like to define a variable and use it in both commands. I tried
DEFINES='-DONE -DTWO -DTHREE'
DD="CPPFLAGS='"
DD+=$DEFINES
DD+="'"
With this,
echo $DD
CPPFLAGS='-DONE -DTWO -DTHREE'
seems to do what I want, but
make one $DD
fails with
make one 'CPPFLAGS='''-DONE' -DTWO '-DTHREE''''
make: invalid option -- 'D'
make: invalid option -- 'T'
make: invalid option -- 'D'
make: invalid option -- 'T'
make: invalid option -- 'H'
Usage: make [options] [target] ...
答案1
得分: 2
不要引用自己。使用 printf "%q"
或 @Q
扩展。
使用 bash 数组。
defines=(
-DONE
-DTWO
-DTHREE
)
make_args=(
"CPPFLAGS=${defines[*]@Q}"
)
make one "${make_args[@]}"
但是
$DD
没有被引用,因此它会进行单词分割扩展,将其分割成空格。
使用 shellcheck 检查你的脚本。
Make 是一个老掌门。考虑使用更新的构建系统,如 CMake。
英文:
Do not quote yourself. Use printf "%q"
or @Q
expansion.
Use bash arrays.
defines=(
-DONE
-DTWO
-DTHREE
)
make_args=(
"CPPFLAGS=${defines[*]@Q}"
)
make one "${make_args[@]}"
> but
$DD
is not quoted, so it undergoes word splitting expansion, which splits it on spaces.
Check your script with shellcheck.
Make is an old grandpa. Consider using newer build system, like CMake.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论