aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--STRINGS.html27
1 files changed, 27 insertions, 0 deletions
diff --git a/STRINGS.html b/STRINGS.html
new file mode 100644
index 0000000..687456f
--- /dev/null
+++ b/STRINGS.html
@@ -0,0 +1,27 @@
+<h1>Bash string manipulation cheatsheet</h1>
+<p><a href="https://gist.github.com/magnetikonline/90d6fe30fc247ef110a1">https://gist.github.com/magnetikonline/90d6fe30fc247ef110a1</a></p>
+<p>| <strong>Assignment</strong> | |
+| :--- | :--- |
+| Assign <code>value</code> to <code>variable</code> if <code>variable</code> is not already set, <code>value</code> is returned. <!-- raw HTML omitted --> <!-- raw HTML omitted -->Combine with a <code>:</code> no-op to discard/ignore return <code>value</code>. | <code>${variable=&quot;value&quot;}</code> <!-- raw HTML omitted --><code>: ${variable=&quot;value&quot;}</code> |
+| <strong>Removal</strong> | |
+| Delete shortest match of <code>needle</code> from front of <code>haystack</code>. | <code>${haystack#needle}</code> |
+| Delete longest match of <code>needle</code> from front of <code>haystack</code>. | <code>${haystack##needle}</code> |
+| Delete shortest match of <code>needle</code> from back of <code>haystack</code>. | <code>${haystack%needle}</code> |
+| Delete longest match of <code>needle</code> from back of <code>haystack</code>. | <code>${haystack%%needle}</code> |
+| <strong>Replacement</strong> | |
+| Replace first match of <code>needle</code> with <code>replacement</code> from <code>haystack</code>. | <code>${haystack/needle/replacement}</code> |
+| Replace all matches of <code>needle</code> with <code>replacement</code> from <code>haystack</code>. | <code>${haystack//needle/replacement}</code> |
+| If <code>needle</code> matches front of <code>haystack</code> replace with <code>replacement</code>. | <code>${haystack/#needle/replacement}</code> |
+| If <code>needle</code> matches back of <code>haystack</code> replace with <code>replacement</code>. | <code>${haystack/%needle/replacement}</code> |
+| <strong>Substitution</strong> | |
+| If <code>variable</code> not set, return <code>value</code>, else <code>variable</code>. | <code>${variable-value}</code> |
+| If <code>variable</code> not set <em>or</em> empty, return <code>value</code>, else <code>variable</code>. | <code>${variable:-value}</code> |
+| If <code>variable</code> set, return <code>value</code>, else null string. | <code>${variable+value}</code> |
+| If <code>variable</code> set <em>and</em> not empty, return <code>value</code>, else null string. | <code>${variable:+value}</code> |
+| <strong>Extraction</strong> | |
+| Extract <code>length</code> characters from <code>variable</code> starting at <code>position</code>. | <code>${variable:position:length}</code> |
+| Return string length of <code>variable</code>. | <code>${#variable}</code> |
+| <strong>Escaping</strong> | |
+| Single quotes inside a single quoted string. | <code>echo 'Don'\''t break my escape!'</code> |
+| <strong>Indirection</strong> | |
+| Return value of variable name held in <code>indirect</code>, else <code>value</code>. | <code>indirect=&quot;apple&quot;</code> <!-- raw HTML omitted --><code>apple=&quot;fruit&quot;</code> <!-- raw HTML omitted --><code>${!indirect-value}</code> |</p>