1

In Bash you can check if an environment variable is set with test -v FOO. It doesn't work in Dash on Debian 11 though because its test doesn't support -v:

# test -v FOO
dash: 2: test: -v: unexpected operator

Is there a portable way that works in Bash and Dash (and ideally more shells, but those are the main ones I am concerned with)?

Timmmm
  • 88,195
  • 71
  • 364
  • 509

1 Answers1

2

You can use + in the expansion of the variable, and test it:

test "${FOO+x}"

The expansion results in an empty string if FOO is unset, or x otherwise.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103