≡ Menu

Bash String Manipulation Examples – Length, Substring, Find and Replace

In bash shell, when you use a dollar sign followed by a variable name, shell expands the variable with its value. This feature of shell is called parameter expansion.

But parameter expansion has numerous other forms which allow you to expand a parameter and modify the value or substitute other values in the expansion process. In this article, let us review how to use the parameter expansion concept for string manipulation operations.

This article is part of the on-going bash tutorial series. Refer to our earlier article on bash { } expansion.

1. Identify String Length inside Bash Shell Script


The above format is used to get the length of the given bash variable.

$ cat len.sh
#! /bin/bash

var="Welcome to the geekstuff"

echo ${#var}

$ ./len.sh

To understand more about bash variables, read 6 Practical Bash Global and Local Variable Examples.

2. Extract a Substring from a Variable inside Bash Shell Script

Bash provides a way to extract a substring from a string. The following example expains how to parse n characters starting from a particular position.


Extract substring from $string at $position


Extract $length of characters substring from $string starting from $position. In the below example, first echo statement returns the substring starting from 15th position. Second echo statement returns the 4 characters starting from 15th position. Length must be the number greater than or equal to zero.

$ cat substr.sh
#! /bin/bash

var="Welcome to the geekstuff"

echo ${var:15}
echo ${var:15:4}

$ ./substr.sh

Also, refer to our earlier article to understand more about $*, $@, $#, $$, $!, $?, $-, $_ bash special parameters.

3. Shortest Substring Match

Following syntax deletes the shortest match of $substring from front of $string


Following syntax deletes the shortest match of $substring from back of $string


Following sample shell script explains the above two shortest substring match concepts.

$ cat shortest.sh
#! /bin/bash


echo ${filename#*.}
echo ${filename%.*}

$ ./shortest.sh
After deletion of shortest match from front: string.txt
After deletion of shortest match from back: bash.string

In the first echo statement substring ‘*.’ matches the characters and a dot, and # strips from the front of the string, so it strips the substring “bash.” from the variable called filename. In second echo statement substring ‘.*’ matches the substring starts with dot, and % strips from back of the string, so it deletes the substring ‘.txt’

4. Longest Substring Match

Following syntax deletes the longest match of $substring from front of $string


Following syntax deletes the longest match of $substring from back of $string


Following sample shell script explains the above two longest substring match concepts.

$ cat longest.sh
#! /bin/bash


echo "After deletion of longest match from front:" ${filename##*.}
echo "After deletion of longest match from back:" ${filename%%.*}

$ ./longest.sh
After deletion of longest match from front: txt
After deletion of longest match from back: bash

In the above example, ##*. strips longest match for ‘*.’ which matches “bash.string.” so after striping this, it prints the remaining txt. And %%.* strips the longest match for .* from back which matches “.string.txt”, after striping  it returns “bash”.

5. Find and Replace String Values inside Bash Shell Script

Replace only first match


It matches the pattern in the variable $string, and replace only the first match of the pattern with the replacement.

$ cat firstmatch.sh
#! /bin/bash


echo "After Replacement:" ${filename/str*./operations.}

$ ./firstmatch.sh
After Replacement: bash.operations.txt

Replace all the matches


It replaces all the matches of pattern with replacement.

$ cat allmatch.sh
#! /bin/bash

filename="Path of the bash is /bin/bash"

echo "After Replacement:" ${filename//bash/sh}

$ ./allmatch.sh
After Replacement: Path of the sh is /bin/sh

Taking about find and replace, refer to our earlier articles – sed substitute examples and Vim find and replace.

Replace beginning and end


Following syntax replaces with the replacement string, only when the pattern matches beginning of the $string.


Following syntax replaces with the replacement string, only when the pattern matches at the end of the given $string.

$ cat posmatch.sh
#! /bin/bash


echo "Replaced at the beginning:" ${filename/#\/root/\/tmp}
echo "Replaced at the end": ${filename/%.*/.ksh}

$ ./posmatch.sh
Replaced at the beginning: /tmp/admin/monitoring/process.sh
Replaced at the end: /root/admin/monitoring/process.ksh

Recommended Reading

Bash 101 Hacks, by Ramesh Natarajan. I spend most of my time on Linux environment. So, naturally I’m a huge fan of Bash command line and shell scripting. 15 years back, when I was working on different flavors of *nix, I used to write lot of code on C shell and Korn shell. Later years, when I started working on Linux as system administrator, I pretty much automated every possible task using Bash shell scripting. Based on my Bash experience, I’ve written Bash 101 Hacks eBook that contains 101 practical examples on both Bash command line and shell scripting. If you’ve been thinking about mastering Bash, do yourself a favor and read this book, which will help you take control of your Bash command line and shell scripting.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • balmar July 23, 2010, 11:52 pm

    This is incredibly useful, thanks! You didn’t close the braces at “Replace beginning and end”, and in Example 3 the echo doesn’t contain “After deletion of shortest match from front/back:”.

  • roko July 24, 2010, 7:11 am

    great! thanks a lot for all theses tricks and explanations !

  • Osborn July 25, 2010, 10:51 pm

    This is really helpful for me. Great!

  • Kafi August 24, 2010, 3:19 am

    Thanks man

  • philluder August 26, 2010, 4:05 pm

    If you use bash 4.x you can source the oobash. A string lib written in bash with oo-style:


    String is the constructor function:

    String a abcda

    a.indexOf a


    a.lastIndexOf a


    a.indexOf da


    There are many “methods” more to work with strings in your scripts:

    -base64Decode -base64Encode -capitalize -center
    -charAt -concat -contains -count
    -endsWith -equals -equalsIgnoreCase -reverse
    -hashCode -indexOf -isAlnum -isAlpha
    -isAscii -isDigit -isEmpty -isHexDigit
    -isLowerCase -isSpace -isPrintable -isUpperCase
    -isVisible -lastIndexOf -length -matches
    -replaceAll -replaceFirst -startsWith -substring
    -swapCase -toLowerCase -toString -toUpperCase
    -trim -zfill

  • smikler June 22, 2011, 6:28 am

    Thanks a lot for script!

  • Mukesh Kumar Bhansali July 5, 2011, 3:51 am

    Very Good Article such that I saved its URL for future refrences if I need & Forget.

  • Bhaskar July 27, 2011, 3:06 am

    Thanks a lot for scripts saved my day

  • Tahir October 14, 2011, 7:51 am

    Thanks a lot !!

  • mang February 27, 2012, 2:41 am

    good stuff…

  • arun June 12, 2012, 12:54 am

    please guide me – how to (change/insert/delete) any character from a string by indicating the position

  • jayesh & umesh December 4, 2012, 10:12 am

    thank you

  • Raja February 25, 2013, 11:34 pm


    How to do the following


    #replacement command here

    echo $string1; #should print “somethinggoodsomethinggood”


  • Prabhakar March 21, 2013, 1:27 am

    I have a querry………. please help!!!

    suppose there is a string “hello 14 all -23 I am here”

    Now from this string I want to extarct 14 and -23. How can I do so??????

  • Eric October 7, 2013, 2:10 am

    Hi Ramesh, this guide save me from hell, thanks alot!

  • Misgana October 18, 2013, 10:16 am

    Thank you very much! You presented the simplest and elegant technique.

  • Andres July 9, 2014, 8:47 am

    Excelent information.

    Thanks a lot!

  • Paul July 27, 2014, 8:06 pm

    In the section “Replace beginning and end”, you are missing ‘}’ from the examples.

  • Ramesh Natarajan July 31, 2014, 4:04 pm


    Thanks for catching the missing }. It is fixed now.

  • John March 16, 2015, 5:30 pm

    Thanks much for the useful and easy to follow examples. I really appreciate it!

  • kuldip March 24, 2015, 6:30 am

    Thanks for help …

    Verry using data …

  • Diablo Lamer July 11, 2015, 11:49 am

    Hi my Guru,
    I want to recursively mkdir first before unzip those zip files with the following folder structure:

    I found the problem is how to replace each character “_” with character “/” in order to mkdir recursively and then unzip them to the created folder structure. Could you please help me?

  • dball August 31, 2015, 1:50 pm

    Is there a way I can pull a substring out of a variable using these methods.

    For example

    echo var and just retrieve “domain” from string.
    Obviously I can do this with awk or cut… But just curious if pure bash expansion can do this.

  • Ron April 29, 2016, 7:19 pm

    One thing I noticed is that you put all the commands in a file, which leads newbies to think that they can’t be run directly from the $ prompt.

  • Binh Thanh Nguyen December 14, 2016, 4:35 am

    Thanks, nice tips