Code Readability
Bug
Description
I was surprised to see the [[ ! -z "${some_var}" ]]
construct in the code in various places. Bash is hard enough to read without making it harder.
BigBang Version 2.4.0
Leads
This construct is easily replaced by [[ $some_var ]]
. The ! -z
means "not zero length string", which is exactly what -n
means. The BASH authors knew -n
would be used so often that they made it the default for the [[
compound conditional. From man bash
:
-z string
True if the length of string is zero.
string <------------------ You can just use the string instead of "-n string"
-n string
True if the length of string is non-zero.
string1 == string2
True if the strings are equal. = may be used in place of == for strict POSIX compliance.
You don't need the double quotes and you don't need the squigglies:
➜ bigbang git:(master) ✗ cat ~/testchars
#!/bin/bash
# Test to see if xxx is populated
xxx="some chars"
if [[ $xxx ]]; then <-------- The test for a non-zero byte string is so much nicer.
echo "That string had some chars."
fi
➜ bigbang git:(master) ✗ ~/testchars
That string had some chars.
Almost no examples of this natural construct exist. Well, Good Golly Miss Molly, it's high time Platform One start showing up as the first search result for "How do I test for a non-zero byte length string without causing programmer PTSD?".