diff --git a/src/2024/12/11/variable_is_set.html b/src/2024/12/11/variable_is_set.html new file mode 100644 index 0000000..bfd5ccf --- /dev/null +++ b/src/2024/12/11/variable_is_set.html @@ -0,0 +1,73 @@ + + + + + variable_is_set | For Me + + + + + + + + + + + +
+

For Me

+
+
+
+
+

+
+
+

This is a bash function for checking if a variable is set.

+ +
#!/usr/bin/env bash
+
+set -e
+
+# Careful: this probably will not work in shells other than bash
+variable_is_set()
+{
+	if [[ -n "${!1+x}" ]]; then
+		return 0
+	else
+		return 1
+	fi
+}
+
+variable_is_set FOO || exit 1
+variable_is_set BAR || exit 1
+ +

From my bash manual:

+ +
The `$' character introduces parameter expansion, command substitution, or arithmetic expansion.  The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name.
+
+[...]
+
+If the first character of parameter is an exclamation point, a level of variable indirection is introduced. bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself.  This is known as indirect expansion.
+
+[...]
+
+word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.  When not performing substring expansion, bash tests for a parameter that is unset or null; omitting the colon results in a test only for a parameter that is unset [...].
+
+[...]
+
+${parameter:+word}
+Use Alternate Value.  If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.
+
+[...]
+				
+Conditional expressions are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons. 
+
+-n string
+True if the length of string is non-zero.
+
+
+
+ + + \ No newline at end of file diff --git a/src/index.html b/src/index.html index 06b4fdd..475eea5 100644 --- a/src/index.html +++ b/src/index.html @@ -17,6 +17,10 @@

For Me