DEV Community

abbazs
abbazs

Posted on

3 1

Bash string manipulation

In bash, there are several string manipulation operations that can be used to remove parts of a string based on patterns. Here are some of the most commonly used ones

  • ${variable#pattern}:
    • Removes the shortest match of pattern from the beginning of variable.
   x="abc.def.ghi"
   echo ${x#*.}  # Outputs: def.ghi
Enter fullscreen mode Exit fullscreen mode
  • ${variable##pattern}:
    • Removes the longest match of pattern from the beginning of variable.
   x="abc.def.ghi"
   echo ${x##*.}  # Outputs: ghi
Enter fullscreen mode Exit fullscreen mode
  • ${variable%pattern}:
    • Removes the shortest match of pattern from the end of variable.
   x="abc.def.ghi"
   echo ${x%.*}  # Outputs: abc.def
Enter fullscreen mode Exit fullscreen mode
  • ${variable%%pattern}:
    • Removes the longest match of pattern from the end of variable.
   x="abc.def.ghi"
   echo ${x%%.*}  # Outputs: abc
Enter fullscreen mode Exit fullscreen mode
  • ${variable:offset:length}:
    • Extracts a substring from variable starting at offset and of length length.
   x="abc.def.ghi"
   echo ${x:4:3}  # Outputs: def
Enter fullscreen mode Exit fullscreen mode
  • ${variable/pattern/replacement}:
    • Replaces the first match of pattern with replacement in variable.
   x="abc.def.ghi"
   echo ${x/def/xyz}  # Outputs: abc.xyz.ghi
Enter fullscreen mode Exit fullscreen mode
  • ${variable//pattern/replacement}:
    • Replaces all matches of pattern with replacement in variable.
   x="abc.def.ghi"
   echo ${x//./-}  # Outputs: abc-def-ghi
Enter fullscreen mode Exit fullscreen mode
  • ${variable^pattern}:
    • Converts the first character to uppercase (bash 4.0 and above).
   x="abc"
   echo ${x^}  # Outputs: Abc
Enter fullscreen mode Exit fullscreen mode
  • ${variable^^pattern}:
    • Converts all characters to uppercase (bash 4.0 and above).
   x="abc"
   echo ${x^^}  # Outputs: ABC
Enter fullscreen mode Exit fullscreen mode
  • ${variable,pattern}:

    • Converts the first character to lowercase (bash 4.0 and above).
    x="ABC"
    echo ${x,}  # Outputs: aBC
    
  • ${variable,,pattern}:

    • Converts all characters to lowercase (bash 4.0 and above).
    x="ABC"
    echo ${x,,}  # Outputs: abc
    

These operations provide a powerful and flexible way to manipulate strings directly within bash scripts, allowing for efficient and concise code.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay