-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<title>variable_is_set | For Me</title> | ||
|
||
<link rel="stylesheet" href="/style.css"> | ||
<link rel="canonical" href="https://www.dewhurst.io/2024/12/11/variable_is_set.html"> | ||
|
||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<meta name="description" content="This is a bash function for checking if a variable is set." /> | ||
</head> | ||
|
||
<body> | ||
<header role="banner"> | ||
<h1><a href="/">For Me</a></h1> | ||
</header> | ||
<main> | ||
<article> | ||
<header> | ||
<p><time datetime="2024-12-11T16:49:48.605Z">Dec 11, 2024</time></p> | ||
</header> | ||
<section> | ||
<p>This is a bash function for checking if a variable is set.</p> | ||
|
||
<pre><code>#!/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</code></pre> | ||
|
||
<p>From my bash manual:</p> | ||
|
||
<pre>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. | ||
|
||
[...] | ||
|
||
<u>word</u> 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:+<u>word</u>} | ||
Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of <u>word</u> is substituted. | ||
|
||
[...] | ||
|
||
Conditional expressions are used by the <b>[[</b> compound command and the test and <b>[</b> builtin commands to test file attributes and perform string and arithmetic comparisons. | ||
|
||
-n string | ||
True if the length of string is non-zero.</pre> | ||
</section> | ||
</article> | ||
</main> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,3 +43,8 @@ a { | |
time { | ||
color: slategray; | ||
} | ||
|
||
pre { | ||
text-wrap: wrap; | ||
margin-left: 1em; | ||
} |