Advertisement
mindthump

Bash String Operations

Jul 19th, 2020 (edited)
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.12 KB | None | 0 0
  1. Expression                                 Meaning
  2. ----------                                 -------
  3. ${#string}                                 Length of $string
  4. ${string:position}                         Extract substring from $string at $position
  5. ${string:position:length}                  Extract $length characters substring from $string at $position [zero-indexed, first character is at position 0]
  6. ${string#substring}                        Strip shortest match of $substring from start of $string
  7. ${string##substring}                       Strip longest match of $substring from start of $string
  8. ${string%substring}                        Strip shortest match of $substring from end of $string
  9. ${string%%substring}                       Strip longest match of $substring from end of $string
  10. ${string/substring/replacement}            Replace first match of $substring with $replacement
  11. ${string//substring/replacement}           Replace all matches of $substring with $replacement
  12. ${string/#substring/replacement}           If $substring matches start of $string, substitute $replacement for $substring
  13. ${string/%substring/replacement}           If $substring matches end of $string, substitute $replacement for $substring
  14. expr match "$string" '$substring'          Length of matching $substring* at beginning of $string
  15. expr "$string" : '$substring'              Length of matching $substring* at beginning of $string
  16. expr index "$string" $substring            Numerical position in $string of first character in $substring* that matches [0 if no match, first character counts as position 1]
  17. expr substr $string $position $length      Extract $length characters from $string starting at $position [0 if no match, first character counts as position 1]
  18. expr match "$string" '\($substring\)'      Extract $substring*, searching from beginning of $string
  19. expr "$string" : '\($substring\)'          Extract $substring*, searching from beginning of $string
  20. expr match "$string" '.*\($substring\)'    Extract $substring*, searching from end of $string
  21. expr "$string" : '.*\($substring\)'        Extract $substring*, searching from end of $string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement