0

I searched this site with Google for an answer that explains how to test if a variable is set or not in a shell (I'm using bash, but I'd prefer the solution to be portable) in the strictest sense of the word, but I failed to find any results.

The problem is that the common answers do not actually check to determine whether a variable is set or not. Instead, they check to see if it is empty or not. If I type var="", then var is set to an empty string. Nonetheless, it has still been set. Running [ -z "${var}" ] will return true despite the variable existing.

How do I test if a variable is set or not across various shells as opposed to just empty or not?

Melab
  • 2,594
  • 7
  • 30
  • 51
  • Upon closer inspection, this looks like a duplicate of https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash, I'll remove my (redundant) answer. – Benjamin W. Jun 19 '17 at 21:34
  • Possible duplicate of [How to check if a variable is set in Bash?](https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash) – Benjamin W. Jun 19 '17 at 21:35
  • The top answer gives a POSIX sh way, [this answer](https://stackoverflow.com/a/3601587/3266847) gives a Bash >= 4.2 way. – Benjamin W. Jun 19 '17 at 21:36
  • @BenjaminW., do you mean the one below that one? https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash/17538964#17538964 – ilkkachu Jun 19 '17 at 21:59

0 Answers0