I have CMD+=" -S $1", so if $1 has any spaces in it the parsing will be wrong.
CMD+=" -S $1"
$1
Now, I know this about my script and will be careful not to do that, but it’s still a risk.
Ah, I see.
It may be worthwhile to instead define CMD as an array, rather than a string, like this
CMD
CMD=("do_some_thing") if [[ "$USE_S_FLAG" == 1 ]]; then CMD+=("-S", "$1") fi "${CMD[@]}" "the" "rest" "of" "your" "args"
Of course at that point you’re losing some of the readability benefits of using bash in the first place...
Edit: or, of course, you keep the script simple and readable at the cost of some duplication, e.g.
if [[ "$USE_S_FLAG" == 1 ]]; then "$CMD" -S "$1" "the" "rest" "of" "your" "args" else "$CMD" "the" "rest" "of" "your" "args" fi
I have
CMD+=" -S $1"
, so if$1
has any spaces in it the parsing will be wrong.Now, I know this about my script and will be careful not to do that, but it’s still a risk.
Ah, I see.
It may be worthwhile to instead define
CMD
as an array, rather than a string, like thisOf course at that point you’re losing some of the readability benefits of using bash in the first place...
Edit: or, of course, you keep the script simple and readable at the cost of some duplication, e.g.