Quantcast
Channel: How to determine if a bash variable is empty? - Server Fault
Viewing all articles
Browse latest Browse all 16

Answer by errant.info for How to determine if a bash variable is empty?

$
0
0

An alternate I've seen to [ -z "$foo" ] is the following, however I'm not sure why people use this method, anyone know?

[ "x${foo}" = "x" ]

Anyway if you're disallowing unset variables (either by set -u or set -o nounset), then you'll run into trouble with both of those methods. There's a simple fix to this:

[ -z "${foo:-}" ]

Note: this will leave your variable undef.


Viewing all articles
Browse latest Browse all 16

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>