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.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

đź‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay