Skip to content

Instantly share code, notes, and snippets.

@SimpleJian
Forked from magnetikonline/README.md
Created September 8, 2019 10:15
Show Gist options
  • Save SimpleJian/09151053971b4df8ad1fa5b4532da458 to your computer and use it in GitHub Desktop.
Save SimpleJian/09151053971b4df8ad1fa5b4532da458 to your computer and use it in GitHub Desktop.

Revisions

  1. @magnetikonline magnetikonline revised this gist May 18, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    <th colspan="2">Assignment</th>
    </tr>
    <tr>
    <td>Assign <code>value</code> to <code>variable</code> if <code>variable</code> is not already set. Value will be returned. Couple with `:` no-op if return value is to be discarded.</td>
    <td>Assign <code>value</code> to <code>variable</code> if <code>variable</code> is not already set. Value will be returned.<br /><br />Couple with <code>:</code> no-op if return value is to be discarded.</td>
    <td><code>${variable="value"}</code><br /><code>: ${variable="value"}</code></td>
    </tr>
    <tr>
  2. @magnetikonline magnetikonline revised this gist May 18, 2018. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,13 @@
    # Bash string manipulation cheatsheet

    <table>
    <tr>
    <th colspan="2">Assignment</th>
    </tr>
    <tr>
    <td>Assign <code>value</code> to <code>variable</code> if <code>variable</code> is not already set. Value will be returned. Couple with `:` no-op if return value is to be discarded.</td>
    <td><code>${variable="value"}</code><br /><code>: ${variable="value"}</code></td>
    </tr>
    <tr>
    <th colspan="2">Removal</th>
    </tr>
  3. @magnetikonline magnetikonline revised this gist Aug 19, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -73,5 +73,5 @@

    ## Reference
    - http://tldp.org/LDP/abs/html/string-manipulation.html.
    - http://www.tldp.org/LDP/abs/html/parameter-substitution.html.
    - http://tldp.org/LDP/abs/html/parameter-substitution.html.
    - http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02.
  4. @magnetikonline magnetikonline revised this gist Apr 8, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -65,6 +65,10 @@
    <td>Extract <code>length</code> characters from <code>variable</code> starting at <code>position</code></td>
    <td><code>${variable:position:length}</code></td>
    </tr>
    <tr>
    <td>String length of <code>variable</code></td>
    <td><code>${#variable}</code></td>
    </tr>
    </table>

    ## Reference
  5. @magnetikonline magnetikonline revised this gist Feb 22, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -43,11 +43,11 @@
    <th colspan="2">Substitution</th>
    </tr>
    <tr>
    <td>If <code>variable</code> not set, return <code>value</code></td>
    <td>If <code>variable</code> not set, return <code>value</code>, else <code>variable</code></td>
    <td><code>${variable-value}</code></td>
    </tr>
    <tr>
    <td>If <code>variable</code> not set <em>or</em> empty, return <code>value</code></td>
    <td>If <code>variable</code> not set <em>or</em> empty, return <code>value</code>, else <code>variable</code></td>
    <td><code>${variable:-value}</code></td>
    </tr>
    <tr>
  6. @magnetikonline magnetikonline revised this gist Feb 22, 2016. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -47,9 +47,17 @@
    <td><code>${variable-value}</code></td>
    </tr>
    <tr>
    <td>If <code>variable</code> set, return <code>value</code>, else return null string</td>
    <td>If <code>variable</code> not set <em>or</em> empty, return <code>value</code></td>
    <td><code>${variable:-value}</code></td>
    </tr>
    <tr>
    <td>If <code>variable</code> set, return <code>value</code>, else null string</td>
    <td><code>${variable+value}</code></td>
    </tr>
    <tr>
    <td>If <code>variable</code> set <em>and</em> not empty, return <code>value</code>, else null string</td>
    <td><code>${variable:+value}</code></td>
    </tr>
    <tr>
    <th colspan="2">Extraction</th>
    </tr>
  7. @magnetikonline magnetikonline revised this gist Oct 22, 2015. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -50,6 +50,13 @@
    <td>If <code>variable</code> set, return <code>value</code>, else return null string</td>
    <td><code>${variable+value}</code></td>
    </tr>
    <tr>
    <th colspan="2">Extraction</th>
    </tr>
    <tr>
    <td>Extract <code>length</code> characters from <code>variable</code> starting at <code>position</code></td>
    <td><code>${variable:position:length}</code></td>
    </tr>
    </table>

    ## Reference
  8. @magnetikonline magnetikonline revised this gist Oct 15, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -43,11 +43,11 @@
    <th colspan="2">Substitution</th>
    </tr>
    <tr>
    <td>If <code>variable</code> not set, use <code>fallback</code></td>
    <td><code>${variable-fallback}</code></td>
    <td>If <code>variable</code> not set, return <code>value</code></td>
    <td><code>${variable-value}</code></td>
    </tr>
    <tr>
    <td>If <code>variable</code> set, use <code>value</code>, else use null string</td>
    <td>If <code>variable</code> set, return <code>value</code>, else return null string</td>
    <td><code>${variable+value}</code></td>
    </tr>
    </table>
  9. @magnetikonline magnetikonline revised this gist Oct 15, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,7 @@
    </tr>
    <tr>
    <td>If <code>variable</code> set, use <code>value</code>, else use null string</td>
    <td><code>${variable-value}</code></td>
    <td><code>${variable+value}</code></td>
    </tr>
    </table>

  10. @magnetikonline magnetikonline revised this gist Oct 15, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -55,4 +55,4 @@
    ## Reference
    - http://tldp.org/LDP/abs/html/string-manipulation.html.
    - http://www.tldp.org/LDP/abs/html/parameter-substitution.html.
    - http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02
    - http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02.
  11. @magnetikonline magnetikonline created this gist Sep 15, 2015.
    58 changes: 58 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    # Bash string manipulation cheatsheet

    <table>
    <tr>
    <th colspan="2">Removal</th>
    </tr>
    <tr>
    <td>Delete shortest match of <code>needle</code> from front of <code>haystack</code></td>
    <td><code>${haystack#needle}</code></td>
    </tr>
    <tr>
    <td>Delete longest match of <code>needle</code> from front of <code>haystack</code></td>
    <td><code>${haystack##needle}</code></td>
    </tr>
    <tr>
    <td>Delete shortest match of <code>needle</code> from back of <code>haystack</code></td>
    <td><code>${haystack%needle}</code></td>
    </tr>
    <tr>
    <td>Delete longest match of <code>needle</code> from back of <code>haystack</code></td>
    <td><code>${haystack%%needle}</code></td>
    </tr>
    <tr>
    <th colspan="2">Replacement</th>
    </tr>
    <tr>
    <td>Replace first match of <code>needle</code> with <code>replacement</code> from <code>haystack</code></td>
    <td><code>${haystack/needle/replacement}</code></td>
    </tr>
    <tr>
    <td>Replace all matches of <code>needle</code> with <code>replacement</code> from <code>haystack</code></td>
    <td><code>${haystack//needle/replacement}</code></td>
    </tr>
    <tr>
    <td>If <code>needle</code> matches front of <code>haystack</code> replace with <code>replacement</code></td>
    <td><code>${haystack/#needle/replacement}</code></td>
    </tr>
    <tr>
    <td>If <code>needle</code> matches back of <code>haystack</code> replace with <code>replacement</code></td>
    <td><code>${haystack/%needle/replacement}</code></td>
    </tr>
    <tr>
    <th colspan="2">Substitution</th>
    </tr>
    <tr>
    <td>If <code>variable</code> not set, use <code>fallback</code></td>
    <td><code>${variable-fallback}</code></td>
    </tr>
    <tr>
    <td>If <code>variable</code> set, use <code>value</code>, else use null string</td>
    <td><code>${variable-value}</code></td>
    </tr>
    </table>

    ## Reference
    - http://tldp.org/LDP/abs/html/string-manipulation.html.
    - http://www.tldp.org/LDP/abs/html/parameter-substitution.html.
    - http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02