Skip to content

Instantly share code, notes, and snippets.

@UnitedWithCode
Forked from magnetikonline/README.md
Created October 10, 2022 07:34
Show Gist options
  • Save UnitedWithCode/5c6ede29463c12bb56fbd46ee34fb9c5 to your computer and use it in GitHub Desktop.
Save UnitedWithCode/5c6ede29463c12bb56fbd46ee34fb9c5 to your computer and use it in GitHub Desktop.

Revisions

  1. @magnetikonline magnetikonline revised this gist Feb 14, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -86,6 +86,7 @@
    </table>

    ## Reference

    - http://tldp.org/LDP/abs/html/string-manipulation.html
    - http://tldp.org/LDP/abs/html/parameter-substitution.html
    - Special characters:
  2. @magnetikonline magnetikonline revised this gist Sep 5, 2020. 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
    @@ -76,6 +76,13 @@
    <td>String length of <code>variable</code></td>
    <td><code>${#variable}</code></td>
    </tr>
    <tr>
    <th colspan="2">Escaping</th>
    </tr>
    <tr>
    <td>Single quotes inside a single quoted string</td>
    <td><code>echo 'Don'\''t break my escape!'</code></td>
    </tr>
    </table>

    ## Reference
  3. @magnetikonline magnetikonline revised this gist Nov 26, 2019. 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.<br /><br />Couple with <code>:</code> 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, <code>value</code> is returned.<br /><br />Combine with a <code>:</code> no-op to discard/ignore return <code>value</code>.</td>
    <td><code>${variable="value"}</code><br /><code>: ${variable="value"}</code></td>
    </tr>
    <tr>
  4. @magnetikonline magnetikonline revised this gist Nov 16, 2019. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -79,6 +79,9 @@
    </table>

    ## Reference
    - http://tldp.org/LDP/abs/html/string-manipulation.html.
    - http://tldp.org/LDP/abs/html/parameter-substitution.html.
    - http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02.
    - http://tldp.org/LDP/abs/html/string-manipulation.html
    - http://tldp.org/LDP/abs/html/parameter-substitution.html
    - Special characters:
    - `*`: https://www.tldp.org/LDP/abs/html/special-chars.html#ASTERISKREF
    - `?`: https://www.tldp.org/LDP/abs/html/special-chars.html#WILDCARDQU
    - http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02
  5. @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>
  6. @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>
  7. @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.
  8. @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
  9. @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>
  10. @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>
  11. @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
  12. @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>
  13. @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>

  14. @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.
  15. @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