{"id":34832,"date":"2024-01-23T03:40:10","date_gmt":"2024-01-23T03:40:10","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=34832"},"modified":"2024-04-28T04:49:19","modified_gmt":"2024-04-28T04:49:19","slug":"substring-bash","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/","title":{"rendered":"How to Extract Bash Substring? [5 methods]"},"content":{"rendered":"<p style=\"text-align: justify;\">In Bash, a substring is a part of a string derived or extracted from the string. Substring provides powerful features for text manipulation and processing. Substring extraction is essential for text manipulation.<\/p>\n<p style=\"text-align: justify;\">You can use the following methods to extract Bash substring:<\/p>\n<ol>\n<li>Using Bash\u2019s substring expansion: <code class=\"\" data-line=\"\">${input_string:start_index:length}<\/code><\/li>\n<li>Using the \u201ccut\u201d command: <code class=\"\" data-line=\"\">cut -c N-M &lt;&lt;&lt; input_string<\/code><\/li>\n<li>Using the \u201cawk\u201d command: <code class=\"\" data-line=\"\">awk &#039;{print substr($input_string,start_index, length)}&#039;<\/code><\/li>\n<li>Using the \u201cexpr\u201d command:<code class=\"\" data-line=\"\"> expr substr input_string start_index length<\/code><\/li>\n<li>Using the \u201cgrep\u201d command: <code class=\"\" data-line=\"\">echo input_string | grep -o \u201csubstring\u201d<\/code><\/li>\n<\/ol>\n<p style=\"text-align: justify;\">There are two types of bash substring extraction: <strong>index-based<\/strong> and <strong>pattern-based<\/strong>.<\/p>\n<p style=\"text-align: justify;\">In this article, I\u2019ll explain 4 methods of index-based substring extraction and 3 methods of pattern-based substring extraction in Bash. So let&#8217;s get started!<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#A_Index-Based_Substring_Extraction\" >A. Index-Based Substring Extraction<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#1_Using_Bashs_Substring_Expansion\" >1. Using Bash\u2019s Substring Expansion<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#i_From_the_Start_of_the_String\" >i. From the Start of the String<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#ii_From_the_Middle_of_the_String\" >ii. From the Middle of the String<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#iii_From_the_Positive_Index_Position\" >iii. From the Positive Index Position<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#iv_From_the_Negative_Starting_Index_Position\" >iv. From the Negative Starting Index Position<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#2_Using_the_%E2%80%9Ccut%E2%80%9D_command\" >2. Using the \u201ccut\u201d command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#3_Using_the_%E2%80%9Cawk%E2%80%9D_Command\" >3. Using the \u201cawk\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#4_Using_the_%E2%80%9Cexpr%E2%80%9D_command\" >4. Using the \u201cexpr\u201d command<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#B_Pattern-Based_Extraction\" >B. Pattern-Based Extraction<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#1_Using_%E2%80%9Ccut%E2%80%9D_Command\" >1. Using \u201ccut\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#2_Using_%E2%80%9Cawk%E2%80%9D_Command\" >2. Using \u201cawk\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#3_Using_%E2%80%9Cgrep%E2%80%9D_Command\" >3. Using \u201cgrep\u201d Command<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#Common_Issues_of_Bash_Substring_Extraction\" >Common Issues of Bash Substring Extraction<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#1_Off-By-One_Errors\" >1. Off-By-One Errors<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#2_Handling_spaces\" >2. Handling spaces<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#3_Unintended_Option_Interpretation\" >3. Unintended Option Interpretation<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#People_Also_Ask\" >People Also Ask<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#What_are_the_applications_of_substring_in_Bash\" >What are the applications of substring in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#How_do_you_replace_a_substring_in_Bash\" >How do you replace a substring in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#How_to_find_a_substring_in_a_string_in_Bash\" >How to find a substring in a string in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#What_is_the_%E2%80%9Csubstr%E2%80%9D_function_in_Unix\" >What is the \u201csubstr\u201d function in Unix?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"A_Index-Based_Substring_Extraction\"><\/span>A. Index-Based Substring Extraction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Index-based extraction involves extracting a substring from an original string based on specified start and end positions of characters.<strong> Bash strings are zero-indexed.<\/strong> You can extract a substring based on the index in various ways like Bash\u2019s substring expansion, using the \u201ccut\u201d command, using the \u201cawk\u201d command, and using the \u201cexpr\u201d command.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Using_Bashs_Substring_Expansion\"><\/span>1. Using Bash\u2019s Substring Expansion<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The simplest method to extract a substring from a string is to use the expression\u00a0<code class=\"\" data-line=\"\">${string:start_index:length}<\/code> where the string variable holds the main text or string. The <code class=\"\" data-line=\"\">start_index<\/code> denotes the initial position of characters from which the extraction begins, while the <code class=\"\" data-line=\"\">length<\/code> specifies the size of the resulting substring.<\/p>\n<p style=\"text-align: justify;\">You can check the following examples of substring extraction for a clearer understanding of the topic:<\/p>\n<h4><span class=\"ez-toc-section\" id=\"i_From_the_Start_of_the_String\"><\/span>i. From the Start of the String<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">To extract a substring from the start of the string set the \u201cstart_index\u201d value to <strong>0 <\/strong>and specify the \u201clength\u201d as your preference. For example, to extract a substring of length <strong>11 <\/strong>from the starting, you can check the following script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Define a string variable\nstring=&quot;Linuxsimply and Linux&quot;\n\n\n#Print the string variable\nprintf &quot;The main string:\\n$string&quot;\n\n#Extract a substring from the first character of the string\nsubstring=&quot;${string:0:11}&quot;\n\n#Print the substring value\nprintf &quot;\\n\\nThe substring:\\n$substring\\n&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">The syntax <code class=\"\" data-line=\"\">&quot;${string:0:11}&quot;<\/code> extracts a substring from the first character (index <strong>0<\/strong>) and includes the next <strong>11 <\/strong>characters of the <strong>string <\/strong>variable.<\/div><\/div>\n<p style=\"text-align: justify;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-34835 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/1-substring-expansion-1.png\" alt=\"Index-based substring extraction from the start index in Bash.\" width=\"700\" height=\"191\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/1-substring-expansion-1.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/1-substring-expansion-1-300x82.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>The output shows the extracted substring of a specified length from the main string.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"ii_From_the_Middle_of_the_String\"><\/span>ii. From the Middle of the String<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">To extract a substring from the middle of the string set the \u201cstart_index\u201d to any index value rather than <strong>0 <\/strong>and the \u201clast_index\u201d of the string, and specify the \u201clength\u201d.<\/p>\n<p style=\"text-align: justify;\">You can check the following example to extract a substring of length <strong>9<\/strong> from the original <strong>string, <\/strong>starting at index <strong>8:<\/strong><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Define a string variable\nstring=&quot;Extract substring from middle.&quot;\n\n#Print the string variable\nprintf &quot;The main string:\\n$string&quot;\n\n#Extract a substring from the first character of the string\nsubstring=&quot;${string:8:9}&quot;\n\n#Print the substring value\nprintf &quot;\\n\\nThe substring:\\n$substring\\n&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">In the script, <code class=\"\" data-line=\"\">substring=&quot;${string:8:9}&quot;<\/code> extract a substring from the <strong>string <\/strong>variable. The substring is extracted from the index <strong>8 <\/strong>to index <strong>16. <\/strong>As the substring length is 9, the ending index is <code class=\"\" data-line=\"\">8+9-1=16<\/code>.<\/div><\/div>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter wp-image-34836 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/2-substring-expansion-2.png\" alt=\"Index-based substring extraction from the middle of the string using substring expansion.\" width=\"700\" height=\"193\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/2-substring-expansion-2.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/2-substring-expansion-2-300x83.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>The output shows the extracted substring from the middle of the main string.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"iii_From_the_Positive_Index_Position\"><\/span>iii. From the Positive Index Position<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">Positive position refers to the positions or indices counted from the beginning of the string, starting with 0 for the first character.<\/p>\n<p style=\"text-align: justify;\">To extract a string from the positive position, provide the \u201cstart_index\u201d to indicate from which the extraction should begin. Follow the script below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Define a string variable\nstring=&quot;Extract substring from positive starting position.&quot;\n\n#Print the string variable\nprintf &quot;The main string:\\n$string&quot;\n\n#Extract a substring from a positive starting position\nsubstring=&quot;${string:18}&quot;\n\n#Print the substring value\nprintf &quot;\\n\\nThe substring:\\n$substring\\n&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\"> The code snippet <code class=\"\" data-line=\"\">substring=&quot;${string:18}&quot;<\/code> extracts a substring from the variable <strong>string, <\/strong>starting at index 18 and extending to the end of the string as length is not mentioned.<\/div><\/div>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter wp-image-34837 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/3-substring-expansion-3.png\" alt=\"Index-based substring extraction with the positive index in Bash\" width=\"825\" height=\"166\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/3-substring-expansion-3.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/3-substring-expansion-3-300x60.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/3-substring-expansion-3-768x155.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The result displays the substring obtained from the positive starting position and continuing until the end index.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"iv_From_the_Negative_Starting_Index_Position\"><\/span>iv. From the Negative Starting Index Position<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">The negative starting position refers to the character index counted backward of the string, with <strong>-1<\/strong> representing the last character. To extract a substring from the negative starting position use the syntax, <code class=\"\" data-line=\"\">substring=&quot;${string: -start_index: length}&quot;<\/code>.<\/p>\n<p style=\"text-align: justify;\">See the following bash scripts to extract a substring from a negative position:<\/p>\n<ol>\n<li style=\"text-align: justify;\">To simply specify the negative \u201cstart_index\u201d use the code below:\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Define a string variable\nstring=&quot;Extract substring from the negative starting position.&quot;\n\n#Print the string variable\nprintf &quot;The main string:\\n$string&quot;\n\n#Extract a substring from the negative starting position\nsubstring=&quot;${string: -27}&quot;\n\n#Print the substring value\nprintf &quot;\\n\\nThe substring:\\n$substring\\n&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">Here, <code class=\"\" data-line=\"\">substring=&quot;${string: -27}&quot;<\/code> extracts a substring from the negative\u00a0 \u201cstart_index\u201d <strong>-27 <\/strong>which is <code class=\"\" data-line=\"\">n<\/code> and continues until the end of the string<strong>.<\/strong><\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-34838 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/4-substring-expansion-4.png\" alt=\"Index-based substring extraction with the negative index position.\" width=\"825\" height=\"168\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/4-substring-expansion-4.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/4-substring-expansion-4-300x61.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/4-substring-expansion-4-768x156.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The output shows the extracted substring from the negative starting position of the main string.<\/p>\n<\/li>\n<li>\n<p style=\"text-align: justify;\">You can set both \u201cstart_index\u201d and \u201clength\u201d too. In that case, follow the below script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Define a string variable\nstring=&quot;Extract substring from the negative starting position.&quot;\n\n#Print the string variable\nprintf &quot;The main string:\\n$string&quot;\n\n#Extract a substring from the negative starting position\nsubstring=&quot;${string: -27: 8}&quot;\n\n#Print the substring value\nprintf &quot;\\n\\nThe substring:\\n$substring\\n&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-34839 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/4-substring-expansion-4_2.png\" alt=\"Index-based substring extraction with the negative index position and length.\" width=\"825\" height=\"184\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/4-substring-expansion-4_2.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/4-substring-expansion-4_2-300x67.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/4-substring-expansion-4_2-768x171.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The output shows the extracted substring from the negative starting position which is 27th character from the end of the string, with a length of 8 characters.<\/p>\n<\/li>\n<\/ol>\n<h3><span class=\"ez-toc-section\" id=\"2_Using_the_%E2%80%9Ccut%E2%80%9D_command\"><\/span>2. Using the \u201ccut\u201d command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">If you want to extract the Nth to Mth character of a main string using the <strong><a href=\"https:\/\/linuxsimply.com\/cut-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">\u201ccut\u201d command<\/a><\/strong> along with the\u00a0<code class=\"\" data-line=\"\">-c<\/code> option, you can use the syntax <code class=\"\" data-line=\"\">cut -c N-M &lt;&lt;&lt; input_string<\/code>.<\/p>\n<p>Check the following example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">cut -c 9-17&lt;&lt;&lt; &#039;Extract Substring&#039;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">Here, the <strong>cut <\/strong>command with the <code class=\"\" data-line=\"\">-c<\/code> option extracts a substring consisting of characters <strong>9 <\/strong>to <strong>17 <\/strong>from the main string\u00a0<code class=\"\" data-line=\"\">Extract Substring<\/code>.<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-34840 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/5-substring_cut_1.png\" alt=\"Index-based substring extraction using cut command in Bash.\" width=\"825\" height=\"76\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/5-substring_cut_1.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/5-substring_cut_1-300x28.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/5-substring_cut_1-768x71.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The output shows the extracted \u201cSubstring\u201d from the main string \u201cExtract Substring\u201d.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Using_the_%E2%80%9Cawk%E2%80%9D_Command\"><\/span>3. Using the \u201cawk\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <a href=\"https:\/\/linuxsimply.com\/awk-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>awk command<\/strong><\/a> is equipped with a built-in <strong>substr($s, i, n)<\/strong> function that allows to directly invoke the function for obtaining substrings. The &#8220;substr($s, i, n)&#8221; function has three arguments which are <strong>input string (s)<\/strong>, <strong>start index (i)<\/strong>, and <strong>length (n)<\/strong>. The syntax to extract a substring using the \u201cawk\u201d command is as below:<\/p>\n<p><code class=\"\" data-line=\"\">awk &#039;{print substr($s, i, n)}&#039;<\/code><\/p>\n<p style=\"text-align: justify;\">You can check the following example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">awk &#039;{print substr($0, 11, 9)}&#039; &lt;&lt;&lt; &#039;Extract a substring&#039;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">The <strong>awk<\/strong> command extracts a substring from the input string \u2018Extract a substring\u2019. It starts at the <strong>11th<\/strong> character (\u2018s\u2019) and includes the next <strong>9<\/strong> characters.<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-34841 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/6-using-awk-command_index.png\" alt=\"Index-based substring extraction using &quot;awk&quot; command in Bash.\" width=\"825\" height=\"81\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/6-using-awk-command_index.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/6-using-awk-command_index-300x29.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/6-using-awk-command_index-768x75.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The output shows the extracted substring from the input string.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_Using_the_%E2%80%9Cexpr%E2%80%9D_command\"><\/span>4. Using the \u201cexpr\u201d command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <strong>expr command <\/strong>extracts a substring from a string based on a specific starting index and length whose syntax is <code class=\"\" data-line=\"\">expr substr input_string start_index length<\/code>. Here, <strong>substr <\/strong>is a subcommand of <strong>expr. <\/strong>Check the following example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">expr substr &quot;Extracting substring using awk&quot; 12 9<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\"> The <strong>expr <\/strong>command extracts a substring from the given string, starting at position 12 and including the next 9 characters. <\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-34842 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/7-using-awk-command-1.png\" alt=\"Index-based substring extraction using the &quot;expr&quot; command in Bash.\" width=\"825\" height=\"60\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/7-using-awk-command-1.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/7-using-awk-command-1-300x22.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/7-using-awk-command-1-768x56.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The output displays the extracted substring from the main string.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"B_Pattern-Based_Extraction\"><\/span>B. Pattern-Based Extraction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Pattern-based substring extraction in Bash involves using patterns or regular expressions to identify and isolate specific substrings within a larger string. This is usually achieved through tools like \u2018grep\u2019, \u2018sed\u2019, or \u2018awk commands. In this section, 3 ways of pattern-based substring extraction will be discussed.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Using_%E2%80%9Ccut%E2%80%9D_Command\"><\/span>1. Using \u201ccut\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To extract a substring, utilize the &#8220;cut&#8221; command with the <code class=\"\" data-line=\"\">-d<\/code> option to define a delimiter and the <code class=\"\" data-line=\"\">-f<\/code> option to designate the field number of the desired substring. The syntax is, <code class=\"\" data-line=\"\">cut -d &#039;&lt;delimiter&gt;&#039; -f &lt;field_number&gt;<\/code>.<\/p>\n<p style=\"text-align: justify;\">For a pattern-based substring extraction using the cut command, use the following Bash script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Declare a variable\nstring=&quot;Extract Substring&quot;\n\n# Extract the substring\nsubstring1=$(echo ${string} | cut -d &#039; &#039; -f 1)\nsubstring2=$(echo ${string} | cut -d &#039; &#039; -f 2)\n\n#Print the string variable\nprintf &quot;The main string:\\n$string\\n\\n&quot;\n\n# Print the substring\necho &quot;First substring: $substring1&quot;\necho &quot;Second substring: $substring2&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">The <strong>cut <\/strong>command along with the <strong>echo <\/strong>command extracts the fields (substring) from the original<strong> string<\/strong> based on the delimiter (<strong>space<\/strong>), which is specified by the option <code class=\"\" data-line=\"\">-d<\/code>. The <code class=\"\" data-line=\"\">-f<\/code> option specifies which field to extract.<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-34843 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/8-substring_cut_2.png\" alt=\"Pattern-based substring extraction using &quot;cut&quot; command in Bash.\" width=\"600\" height=\"172\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/8-substring_cut_2.png 600w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/8-substring_cut_2-300x86.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/>The output shows the extracted fields from the main string using the space as a delimiter.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_Using_%E2%80%9Cawk%E2%80%9D_Command\"><\/span>2. Using \u201cawk\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">You can utilize the <strong>awk <\/strong>command along with the field separator option <code class=\"\" data-line=\"\">-F<\/code>. Follow the below script to extract pattern-based substring using the <strong>awk <\/strong>command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Define a string variable\nstring=&quot;Try to extract substring using awk.&quot;\n\n#Print the string variable\nprintf &quot;The main string:\\n$string\\n\\n&quot;\n\n#Extract a substring\nawk -F &#039;to |using &#039; &#039;{print $2}&#039; &lt;&lt;&lt; &quot;$string&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\"> Here, <code class=\"\" data-line=\"\">awk -F &#039;to |using &#039;<\/code> sets the field separator <code class=\"\" data-line=\"\">-F<\/code> to a regular expression that matches either &#8220;to &#8221; or &#8220;using &#8220;. As a result, the <strong>awk <\/strong>command treats the text &#8220;to &#8221; and &#8220;using &#8221; as separate fields. So the <strong>string <\/strong>is separated into three fields. The<code class=\"\" data-line=\"\">\u00a0&#039;{print $2}&#039;<\/code> <span style=\"font-size: inherit;\">instructs <\/span><strong style=\"font-size: inherit;\">awk<\/strong><span style=\"font-size: inherit;\"> to print the second field which is the text between\u00a0 &#8220;to&#8221; and &#8220;using&#8221;.<\/span><\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-34844 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/9-using-awk-command_pattern.png\" alt=\"Pattern-based substring extraction using &quot;awk&quot; command in Bash.\" width=\"700\" height=\"169\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/9-using-awk-command_pattern.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/9-using-awk-command_pattern-300x72.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>Here\u2019s the extracted substring shown in the output.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Using_%E2%80%9Cgrep%E2%80%9D_Command\"><\/span>3. Using \u201cgrep\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <a href=\"https:\/\/linuxsimply.com\/grep-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>grep command<\/strong><\/a> can search for a specific substring along with the <code class=\"\" data-line=\"\">-o<\/code> option. Check the following example to extract substring using the \u201cgrep\u201d command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo &quot;Extracting substring using patterns&quot; | grep -o &#039;substring&#039;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">The <strong>grep <\/strong>command with the <code class=\"\" data-line=\"\">-o<\/code> option searches for the specified pattern <code class=\"\" data-line=\"\">substring<\/code> in the input string \u201cExtracting substring using patterns\u201d.<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-34845 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/10-using-grep-command-pattern.png\" alt=\"Pattern-based substring extraction using &quot;grep&quot; command in Bash.\" width=\"825\" height=\"81\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/10-using-grep-command-pattern.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/10-using-grep-command-pattern-300x29.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/10-using-grep-command-pattern-768x75.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The output shows the searched substring from the input string.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Common_Issues_of_Bash_Substring_Extraction\"><\/span>Common Issues of Bash Substring Extraction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">When working with substring and its extraction, users may encounter some common issues like off-by-one errors, handling spaces, and unintended option interpretation. This section will discuss these issues with their corresponding solutions:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Off-By-One_Errors\"><\/span>1. Off-By-One Errors<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">As Bash strings are zero-indexed, sometimes off-by-errors can occur if you start counting from 1 instead of 0. So be mindful that the initial character of the string is located at position 0 to prevent potential off-by-one errors.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_Handling_spaces\"><\/span>2. Handling spaces<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Handling spaces in Bash substring extraction requires careful consideration. Spaces can affect the interpretation of field separators and indices. When handling spaces in Bash:<\/p>\n<ol>\n<li style=\"text-align: justify;\">Use double quotes for variable expansion (<code class=\"\" data-line=\"\">&quot;${variable:start:length}&quot;<\/code>).<\/li>\n<li style=\"text-align: justify;\">Quote arguments in commands to preserve spaces (<code class=\"\" data-line=\"\">cut -d &#039; &#039; -f2<\/code>).<\/li>\n<\/ol>\n<h3><span class=\"ez-toc-section\" id=\"3_Unintended_Option_Interpretation\"><\/span>3. Unintended Option Interpretation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">When utilizing negative indices, it\u2019s important to include a space before the \u2018<strong>&#8211;<\/strong>\u2019. Omitting this space may lead Bash to interpret the negative index (say -10) as an option for the command rather than as a negative index. For example,<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Define the string\nstring=&#039;Bash substring extraction&#039;\n\n#substring extraction\n#with a space before the negative index\nsubstring1=${string: -10}\n\n#without a space before the negative index\nsubstring2=${string:-10}\n\necho &quot;The substring with space before negative index:&quot;\necho $substring1\necho &quot;The substring without space before negative index:&quot;\necho $substring2<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34846\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/11-issue-unintended-error.png\" alt=\"Unintended option error issue in substring extraction.\" width=\"825\" height=\"212\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/11-issue-unintended-error.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/11-issue-unintended-error-300x77.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/11-issue-unintended-error-768x197.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>Here, due to not using space before the negative index, the second substring extraction doesn\u2019t occur and the main string is shown instead of the extracted substring.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In conclusion, mastering substring and its manipulation is a valuable skill for any Bash script developer. It\u2019s necessary for parsing log files and manipulating text or data. This article discusses 4 methods of index-based substring extraction and 3 methods of pattern-based substring extraction. It also shows the common issues with their solutions that can cause problems while working with Bash substring. Hope this guide clears your concepts on the Bash substring and its extraction and eases your advanced approaches.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"People_Also_Ask\"><\/span>People Also Ask<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"What_are_the_applications_of_substring_in_Bash\"><\/span>What are the applications of substring in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Bash substring extraction is commonly used in scripting for various purposes, such as:<\/p>\n<ol>\n<li>Data processing and extraction.<\/li>\n<li>Text manipulation<\/li>\n<li>Data cleaning<\/li>\n<li>String manipulation in automation<\/li>\n<li>Filename manipulation<\/li>\n<\/ol>\n<h3><span class=\"ez-toc-section\" id=\"How_do_you_replace_a_substring_in_Bash\"><\/span>How do you replace a substring in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In Bash, you can replace a substring in a string using the \u2018awk\u2019 command. Here\u2019s a simple example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo &#039;Hello, World!&#039; | awk &#039;{gsub(\/World\/, &quot;Universe&quot;); print}&#039;<\/code><\/pre>\n<p style=\"text-align: justify;\">Here, <strong>gsub <\/strong>function inside the \u201cawk\u201d command searches for the regular expression <strong>\/World\/ <\/strong>in the input text and replaces all occurrences with the string &#8220;Universe&#8221;.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_find_a_substring_in_a_string_in_Bash\"><\/span>How to find a substring in a string in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In Bash, you can find a substring using the <code class=\"\" data-line=\"\">[[ ]]<\/code> operator along with the <code class=\"\" data-line=\"\">*<\/code> wildcard for pattern matching. Here\u2019s an example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&quot;Hello, World!&quot;\nsubstring=&quot;World&quot;\n\nif [[ $string == *&quot;$substringd&quot;* ]]; then\necho &quot;Substring found in the string.&quot;\nelse\necho &quot;Substring not found in the string.&quot;\nfi<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_%E2%80%9Csubstr%E2%80%9D_function_in_Unix\"><\/span>What is the \u201csubstr\u201d function in Unix?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <strong>\u2018substr\u2019 <\/strong>function is a text-processing tool for extracting substring from a string based on a\u00a0 specific starting position and optional length. The syntax is <code class=\"\" data-line=\"\">substr(string, start [, length])<\/code>. In AWK, the \u201csubstr\u201d function is used to extract a portion of a string.<\/p>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": {\n    \"@type\": \"Question\",\n    \"name\": \"What is the \u201csubstr\u201d function in Unix?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"The \u2018substr\u2019 function is a text-processing tool for extracting substring from a string based on a\u00a0 specific starting position and optional length. The syntax is substr(string, start [, length]). In AWK, the \u201csubstr\u201d function is used to extract a portion of a string.\"\n    }\n  }\n}\n<\/script><\/p>\n<p><span style=\"font-size: 18pt; color: #003366;\"><strong>Related Articles<\/strong><\/span><\/p>\n<ul>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/basics\/\" target=\"_blank\" rel=\"noopener\">Bash String Basics<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/operations\/\" target=\"_blank\" rel=\"noopener\">Bash String Operations<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/\" target=\"_blank\" rel=\"noopener\">String Manipulation in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/\" target=\"_blank\" rel=\"noopener\">8 Methods to Split String in Bash [With Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/check-string\/\" target=\"_blank\" rel=\"noopener\">Check String in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/\" target=\"_blank\" rel=\"noopener\">A Complete Guide to Bash Regex<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/multiline-string\/\" target=\"_blank\" rel=\"noopener\">Bash Multiline String<\/a><\/strong><\/li>\n<\/ul>\n<hr \/>\n<p><strong>&lt;&lt; Go Back to <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash String&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/\" target=\"_blank\" rel=\"noopener\">Bash String<\/a><\/span> | <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Scripting Tutorial&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\" target=\"_blank\" rel=\"noopener\">Bash Scripting Tutorial<\/a><\/span><\/strong><\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-center kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;center&quot;,&quot;id&quot;:&quot;34832&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;How to Extract Bash Substring? [5 methods]&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            <span class=\"kksr-muted\">Rate this post<\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>In Bash, a substring is a part of a string derived or extracted from the string. Substring provides powerful features &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How to Extract Bash Substring? [5 methods]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#more-34832\" aria-label=\"Read more about How to Extract Bash Substring? [5 methods]\">Read more<\/a><\/p>\n","protected":false},"author":314912,"featured_media":34847,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[189],"class_list":["post-34832","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-bash-string","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Extract Bash Substring? [5 methods] - LinuxSimply<\/title>\n<meta name=\"description\" content=\"Bash substring is a sequence of characters in a string which can be extracted in various methods like using substring expansion or commands.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Extract Bash Substring? [5 methods] - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"Bash substring is a sequence of characters in a string which can be extracted in various methods like using substring expansion or commands.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/profile.php?id=100077694755838&amp;mibextid=ZbWKwL\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-23T03:40:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-28T04:49:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12-Feature-image.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Auhona Islam\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Auhona Islam\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/substring\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/substring\\\/\"},\"author\":{\"name\":\"Auhona Islam\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/25a3d16db4655804617c9e0c4a7989fb\"},\"headline\":\"How to Extract Bash Substring? [5 methods]\",\"datePublished\":\"2024-01-23T03:40:10+00:00\",\"dateModified\":\"2024-04-28T04:49:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/substring\\\/\"},\"wordCount\":1774,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/substring\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/12-Feature-image.png\",\"keywords\":[\"bash string\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/substring\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/substring\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/substring\\\/\",\"name\":\"How to Extract Bash Substring? [5 methods] - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/substring\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/substring\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/12-Feature-image.png\",\"datePublished\":\"2024-01-23T03:40:10+00:00\",\"dateModified\":\"2024-04-28T04:49:19+00:00\",\"description\":\"Bash substring is a sequence of characters in a string which can be extracted in various methods like using substring expansion or commands.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/substring\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/substring\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/substring\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/12-Feature-image.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/12-Feature-image.png\",\"width\":400,\"height\":400,\"caption\":\"Substring extraction in Bash\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/substring\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bash Scripting Tutorial\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Bash String\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"How to Extract Bash Substring? [5 methods]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"name\":\"LinuxSimply\",\"description\":\"All About Linux\",\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/linuxsimply.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\",\"name\":\"LinuxSimply\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"width\":355,\"height\":48,\"caption\":\"LinuxSimply\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/25a3d16db4655804617c9e0c4a7989fb\",\"name\":\"Auhona Islam\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Auhona-Islam-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Auhona-Islam-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Auhona-Islam-II-96x96.png\",\"caption\":\"Auhona Islam\"},\"description\":\"Auhona Islam is a dedicated professional with a background in Electronics and Communication Engineering (ECE) from Khulna University of Engineering &amp; Technology. Graduating in 2023, Auhona is currently excelling in her role as a Linux content developer executive at SOFTEKO to provide a more straightforward route for Linux users. She aims to generate compelling materials for Linux users with her knowledge and skills. She holds her enthusiasm in the realm of Machine Learning (ML), Deep Learning (DL), and Artificial Intelligence (AI). Apart from these, she has a passion for playing instruments and singing. Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/profile.php?id=100077694755838&mibextid=ZbWKwL\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/auhona-islam-429822208\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/auhona\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Extract Bash Substring? [5 methods] - LinuxSimply","description":"Bash substring is a sequence of characters in a string which can be extracted in various methods like using substring expansion or commands.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/","og_locale":"en_US","og_type":"article","og_title":"How to Extract Bash Substring? [5 methods] - LinuxSimply","og_description":"Bash substring is a sequence of characters in a string which can be extracted in various methods like using substring expansion or commands.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/profile.php?id=100077694755838&mibextid=ZbWKwL","article_published_time":"2024-01-23T03:40:10+00:00","article_modified_time":"2024-04-28T04:49:19+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12-Feature-image.png","type":"image\/png"}],"author":"Auhona Islam","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Auhona Islam","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/"},"author":{"name":"Auhona Islam","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/25a3d16db4655804617c9e0c4a7989fb"},"headline":"How to Extract Bash Substring? [5 methods]","datePublished":"2024-01-23T03:40:10+00:00","dateModified":"2024-04-28T04:49:19+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/"},"wordCount":1774,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12-Feature-image.png","keywords":["bash string"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/","name":"How to Extract Bash Substring? [5 methods] - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12-Feature-image.png","datePublished":"2024-01-23T03:40:10+00:00","dateModified":"2024-04-28T04:49:19+00:00","description":"Bash substring is a sequence of characters in a string which can be extracted in various methods like using substring expansion or commands.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12-Feature-image.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12-Feature-image.png","width":400,"height":400,"caption":"Substring extraction in Bash"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linuxsimply.com\/"},{"@type":"ListItem","position":2,"name":"Bash Scripting Tutorial","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/"},{"@type":"ListItem","position":3,"name":"Bash String","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/"},{"@type":"ListItem","position":4,"name":"How to Extract Bash Substring? [5 methods]"}]},{"@type":"WebSite","@id":"https:\/\/linuxsimply.com\/#website","url":"https:\/\/linuxsimply.com\/","name":"LinuxSimply","description":"All About Linux","publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxsimply.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxsimply.com\/#organization","name":"LinuxSimply","url":"https:\/\/linuxsimply.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","width":355,"height":48,"caption":"LinuxSimply"},"image":{"@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/25a3d16db4655804617c9e0c4a7989fb","name":"Auhona Islam","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Auhona-Islam-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Auhona-Islam-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Auhona-Islam-II-96x96.png","caption":"Auhona Islam"},"description":"Auhona Islam is a dedicated professional with a background in Electronics and Communication Engineering (ECE) from Khulna University of Engineering &amp; Technology. Graduating in 2023, Auhona is currently excelling in her role as a Linux content developer executive at SOFTEKO to provide a more straightforward route for Linux users. She aims to generate compelling materials for Linux users with her knowledge and skills. She holds her enthusiasm in the realm of Machine Learning (ML), Deep Learning (DL), and Artificial Intelligence (AI). Apart from these, she has a passion for playing instruments and singing. Read Full Bio","sameAs":["https:\/\/www.facebook.com\/profile.php?id=100077694755838&mibextid=ZbWKwL","https:\/\/www.linkedin.com\/in\/auhona-islam-429822208"],"url":"https:\/\/linuxsimply.com\/author\/auhona\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/34832","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/users\/314912"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=34832"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/34832\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/34847"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=34832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=34832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=34832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}