How to extract substring in Bash Shell

In this tutorial, How to extract substring in Bash Shell on Linux. You can use a select substring from the string. The awk command has a few useful functions. It is a substr function.

Function substr mean Returns a substring.

The syntax extract substring in Bash Shell

substr(S,P,N)
  • P: starting at position
  • N: it is returns N number of character’s from string S.

For example, I will use a string “HuuPV2123456789” with a length is 15 and starting position is 10.

$ echo "HuuPV2123456789" | awk '{ printf "%s", substr($0, 10, (length($0)-9)) }'

The result is below

456789

man page function substr here. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

1 thought on “How to extract substring in Bash Shell

  1. $ VAR=HuuPV2123456789
    $ echo ${VAR:9}
    456789
    $ echo ${VAR:9:3}
    456
    $ echo ${VAR:9:-1}
    45678

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.