Bash strings handling Examples

Saturday, April 20, 2024 0 Comments

Recently I found some difficulty performing some string handling in shell script especially in bash.
Even If I google for any string handling in shell script, I am bombarded  with a lot of stuff but with tools like awk, perl, sed, python but very rarely in bash. So I thought it would be good to have useful string handling at one place. Below are some use case I would like to show:

  1. Length of a string.
  2. Substring  from a particular position till the end.
  3. Substring  from one position to another inside the same string.
  4. Delete a shortest substring(if exists) from  front of a string.
  5. Delete a shortest substring(if exists) from  back of a string.
  6. Delete a longest substring(if exists) from  front of a string.
  7. Delete a longest substring(if exists) from  back of a string.
  8. Find and Replace single match
  9. Find and Replace all match
Note:The red ones are more frequently used and more confusing as well.

Length of a string




0 comments: