Skip to content

Commit

Permalink
Add variable_is_set
Browse files Browse the repository at this point in the history
  • Loading branch information
edew committed Dec 12, 2024
1 parent b2c9cb6 commit f0a9437
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/2024/12/11/variable_is_set.html
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>
4 changes: 4 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ <h1><a href="/">For Me</a></h1>
</header>
<main>
<ul>
<li>
<a href="/2024/12/11/variable_is_set.html">variable_is_set</a>
<div><time datetime="2024-12-11T16:49:48.605Z">Dec 11, 2024</time></div>
</li>
<li>
<a href="/2024/04/29/simple_baguette.html">Simple Baguette</a>
<div><time datetime="2024-04-29T18:49:00+01:00">Apr 29, 2024</time></div>
Expand Down
5 changes: 5 additions & 0 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ a {
time {
color: slategray;
}

pre {
text-wrap: wrap;
margin-left: 1em;
}

0 comments on commit f0a9437

Please sign in to comment.