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 and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. 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.

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