Skip to main content
make sure `$area` is otherwise unset to start with. Use + instead of :+ to be able to specify an empty list of areas.
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

For your specific use case, there's a much easier answer:

unset -v area
if __SOME_SETTING__; then
  area="ca,us"
fi

process_data -i /some/path ${area:+area+ --area "$area" }

If for some reason you can't use arrays, and you need more control than the above practice offers, you might consider using a function wrapper.

withOptionalArea() {
  if __SOME_SETTING__; then
    "$@" --area us,ca
  else
    "$@"
  fi
}

withOptionalArea process_data -i /some/path

...but this should only be necessary in exceptional circumstances.


Really, use an array.

For your specific use case, there's a much easier answer:

if __SOME_SETTING__; then
  area="ca,us"
fi

process_data -i /some/path ${area:+ --area "$area" }

If for some reason you can't use arrays, and you need more control than the above practice offers, you might consider using a function wrapper.

withOptionalArea() {
  if __SOME_SETTING__; then
    "$@" --area us,ca
  else
    "$@"
  fi
}

withOptionalArea process_data -i /some/path

...but this should only be necessary in exceptional circumstances.


Really, use an array.

For your specific use case, there's a much easier answer:

unset -v area
if __SOME_SETTING__; then
  area="ca,us"
fi

process_data -i /some/path ${area+ --area "$area" }

If for some reason you can't use arrays, and you need more control than the above practice offers, you might consider using a function wrapper.

withOptionalArea() {
  if __SOME_SETTING__; then
    "$@" --area us,ca
  else
    "$@"
  fi
}

withOptionalArea process_data -i /some/path

...but this should only be necessary in exceptional circumstances.


Really, use an array.

Source Link
Charles Duffy
  • 1.9k
  • 18
  • 22

For your specific use case, there's a much easier answer:

if __SOME_SETTING__; then
  area="ca,us"
fi

process_data -i /some/path ${area:+ --area "$area" }

If for some reason you can't use arrays, and you need more control than the above practice offers, you might consider using a function wrapper.

withOptionalArea() {
  if __SOME_SETTING__; then
    "$@" --area us,ca
  else
    "$@"
  fi
}

withOptionalArea process_data -i /some/path

...but this should only be necessary in exceptional circumstances.


Really, use an array.