How to extract substring in Bash Shell

In this tutorial, How to extract substring in Bash Shell on Linux. You can use 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

  • 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 length is 15 and starting positions is 10.

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

The result as below


man page funtion substr here

, , , ,

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 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}
    $ echo ${VAR:9:3}
    $ echo ${VAR:9:-1}

Leave a Reply

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

Comment moderation is enabled. Your comment may take some time to appear.

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