DEV Community

abbazs
abbazs

Posted on • Edited on

1 1

Bash string manipulation 2

Bash string manipulation involves using parameter expansion to modify the value of a variable. Here are some common string manipulation techniques in bash:

Removing a substring from the front of a string

Remove shortest match of a pattern from the front

var="System Commands"
echo ${var#* }
Enter fullscreen mode Exit fullscreen mode

This removes the shortest match of * (anything followed by a space) from the beginning of the string, resulting in Commands.

Remove longest match of a pattern from the front

var="System Commands are useful"
echo ${var##* }
Enter fullscreen mode Exit fullscreen mode

This removes the longest match of * from the beginning of the string, resulting in useful.

Removing a substring from the end of a string

Remove shortest match of a pattern from the end

var="System Commands"
echo ${var% *}
Enter fullscreen mode Exit fullscreen mode

This removes the shortest match of * (space followed by anything) from the end of the string, resulting in System.

Remove longest match of a pattern from the end

var="System Commands are useful"
echo ${var%% *}
Enter fullscreen mode Exit fullscreen mode

This removes the longest match of * from the end of the string, resulting in System.

Extracting a substring

Extract substring from position

var="System Commands"
echo ${var:0:6}
Enter fullscreen mode Exit fullscreen mode

This extracts 6 characters starting from position 0, resulting in System.

Length of a string

var="System Commands"
echo ${#var}
Enter fullscreen mode Exit fullscreen mode

This returns the length of the string, which is 15.

Replacing part of a string

Replace first match

var="System Commands"
echo ${var/Commands/Operations}
Enter fullscreen mode Exit fullscreen mode

This replaces the first occurrence of Commands with Operations, resulting in System Operations.

Replace all matches

var="System Commands Commands"
echo ${var//Commands/Operations}
Enter fullscreen mode Exit fullscreen mode

This replaces all occurrences of Commands with Operations, resulting in System Operations Operations.

Example for extracting the first word using string manipulation

To extract the first word from a string, you can use:

var="System Commands"
echo ${var%% *}
Enter fullscreen mode Exit fullscreen mode

This removes everything from the first space to the end of the string, effectively extracting the first word, resulting in System.

These techniques allow you to manipulate strings in various ways directly within bash scripts, providing powerful tools for text processing without needing external utilities.

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

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

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay