aboutsummaryrefslogtreecommitdiff
path: root/STRINGS.html
blob: 687456f010ab44a98a737e3acda7196a43f116dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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>