Skip to content

Feature Detection Is Better than Version Detection

andychu edited this page Jun 25, 2019 · 12 revisions

Summary: Version Detection like checking $BASH_VERSION or User-Agent == "Mozilla" is bad for portability.

TODO: Fill out this page more.

NOTE: OSH won't pretend it's bash! It is largely compatible with bash, but not identical.

How Do I Use Feature Detection?

In shell (or JavaScript), an easy and effective way is to use eval. (This is perhaps the best reason to use eval!)

For example, you can test if a shell has declare with something like

if eval 'declare myvar'; then
  have_declare=yes
else
  have_declare=''
fi

# usage:

if test -n "$have_declare"; then
  ...
fi
Clone this wiki locally