String-Funktionen und Patternmatching in Bash

String-Funktionen und Patternmatching in Bash nutze ich eher selten und bin daher immer froh, wenn ich rasch etwas zum Nachschlagen finde. Diesmal hat mir dieser Artikel weitergeholfen der in einem Kommentar diese ergänzende und mir sehr hilfreiche Tabelle enthielt:
[code]
${var:pos[:len]} # extract substr from pos (0-based) for len
${var/substr/repl} # replace first match
${var//substr/repl} # replace all matches
${var/#substr/repl} # replace if matches at beginning (non-greedy)
${var/##substr/repl} # replace if matches at beginning (greedy)
${var/%substr/repl} # replace if matches at end (non-greedy)
${var/%%substr/repl} # replace if matches at end (greedy)
${#var} # returns length of $var
${!var} # indirect expansion
[/code]

Verwandte weiterführende Infos gibt’s in meinem Artikel “Reguläre Ausdrücke – kurz und bündig“.

3 comments Write a comment

  1. Pingback: String ersetzen in Shell - Sonstige Sprachen @ tutorials.de: Forum, Tutorial, Anleitung, Schulung & Hilfe

  2. If you use bash 4.x you can source the oobash. A string lib written in bash with oo-style:

    http://sourceforge.net/projects/oobash/

    String is the constructor function:

    String a abcda

    a.indexOf a

    0

    a.lastIndexOf a

    4

    a.indexOf da

    3

    There are many “methods” more to work with strings in your scripts:

    -base64Decode -base64Encode -capitalize -center
    -charAt -concat -contains -count
    -endsWith -equals -equalsIgnoreCase -reverse
    -hashCode -indexOf -isAlnum -isAlpha
    -isAscii -isDigit -isEmpty -isHexDigit
    -isLowerCase -isSpace -isPrintable -isUpperCase
    -isVisible -lastIndexOf -length -matches
    -replaceAll -replaceFirst -startsWith -substring
    -swapCase -toLowerCase -toString -toUpperCase
    -trim -zfill

  3. Danke für den Beitrag. Diese einfache Alternative für das Ersetzen bzw. Manipulieren von Strings an Stelle von regex und Co war mir bisher unbekannt.