-
-
Notifications
You must be signed in to change notification settings - Fork 158
Feature Detection Is Better than Version Detection
andychu edited this page Jun 25, 2019
·
7 revisions
Summary: Version Detection like checking $BASH_VERSION == "4.4"
or User-Agent == "Mozilla"
is bad for portability.
Instead, it's better to simply use the features that you need, and give good errors when they don't work. You can also explicitly detect them before using them (often with eval
).
TODO: Fill out this page more.
-
Comment on Hacker News Thread: Bash Completion in JSON Fields
- An unnecessary
$BASH_VERSION
check --eval 'complete ...'
would suffice.
- An unnecessary
- All browsers pretend they're Mozilla because web servers detect it
- LLVM Has to Pretend It's GCC to compile code in the wild
- Despite its flaws, GNU autoconf (
./configure
) works because it uses feature detection. If it had used version detection, it would have collapsed from complexity a long time ago, and been a lot less reliable.
NOTE: OSH won't pretend it's bash! It is largely compatible with bash, but not identical.
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