{"id":33986,"date":"2024-01-16T12:05:52","date_gmt":"2024-01-16T12:05:52","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=33986"},"modified":"2024-04-28T04:48:40","modified_gmt":"2024-04-28T04:48:40","slug":"bash-split-string","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/","title":{"rendered":"8 Methods to Split String in Bash [With Examples]"},"content":{"rendered":"<p style=\"text-align: justify;\"><strong>Bash split string<\/strong> refers to slicing a string into several parts based on a specific symbol, character, or substring as a delimiter. Here, a <strong>delimiter<\/strong> is a specific character or sequence of characters used to separate a string. The steps to split a string in Bash are:<\/p>\n<ol>\n<li style=\"text-align: justify;\">Initialize a string variable with any sentence or text. You can also read text from any file or user input.<\/li>\n<li style=\"text-align: justify;\">Choose a separator based on which you want to split the string.<\/li>\n<li style=\"text-align: justify;\">Read the string and Split it using the IFS variable,<strong> readarray<\/strong> command or tr command, etc.<\/li>\n<li style=\"text-align: justify;\">Store the split-ted words either into an array or into a variable.<\/li>\n<li style=\"text-align: justify;\">Finally, print split-ted words using <strong><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/\" target=\"_blank\" rel=\"noopener\">loop<\/a> , <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/input-output\/output\/echo-command\/\" target=\"_blank\" rel=\"noopener\">echo<\/a>, or printf <\/strong>commands.<\/li>\n<\/ol>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/Practice-Files_Bash-Split-String.zip\" class=\"su-button su-button-style-default\" style=\"color:#000000;background-color:#E8F9FF;border-color:#bac8cc;border-radius:56px\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"color:#000000;padding:8px 26px;font-size:20px;line-height:30px;border-color:#effbff;border-radius:56px;text-shadow:0px 0px 0px #000000\"><i class=\"sui sui-download\" style=\"font-size:20px;color:#1AA1D6\"><\/i> Practice Files to Split String in Bash<\/span><\/a><\/div>\n<p>You can use the following 8 methods to split strings in Bash:<\/p>\n<ol>\n<li>Using <strong>IFS<\/strong> variable:\n<pre><code class=\"language-bash\" data-line=\"\">IFS=&#039;Delimiter&#039;\nread -a &lt;array_name&gt; &lt;&lt;&lt; &lt;string_variable&gt;<\/code><\/pre>\n<\/li>\n<li>Using &#8220;<strong>readarray&#8221; <\/strong>command:\n<pre><code class=\"language-bash\" data-line=\"\">readarray -d &quot;delimiter&quot; -t &lt;array_name&gt; &lt;&lt;&lt; &lt;string_variable&gt;<\/code><\/pre>\n<\/li>\n<li>Using <strong>parameter <\/strong>expansion:\n<pre><code class=\"language-bash\" data-line=\"\">echo &quot;${STRING_VARIABLE#* DELIMITER}&quot;\necho &quot;${STRING_VARIABLE##* DELIMITER}&quot;\necho &quot;${STRING_VARIABLE% DELIMITER*}&quot;\necho &quot;${STRING_VARIABLE%% DELIMITER*}&quot;<\/code><\/pre>\n<\/li>\n<li>Using <strong>positional <\/strong>parameters:\n<pre><code class=\"language-bash\" data-line=\"\">set -- $STRING_VARIABLE<\/code><\/pre>\n<\/li>\n<li>Using &#8220;<strong>tr&#8221; <\/strong>command:\n<pre><code class=\"language-bash\" data-line=\"\">array_name=($(echo $STRING_VARIABLE | tr &quot;DELIMITER&quot; &quot;REPLACING_DELIMITER&quot;))<\/code><\/pre>\n<\/li>\n<li>Using &#8220;<strong>awk&#8221; <\/strong>command:\n<pre><code class=\"language-bash\" data-line=\"\">echo &quot;$STRING_VARIABLE&quot; | awk -F &#039; DELIMITER&#039; &#039;{print $1,$2,$3,...field\u2019s positional value}&#039;<\/code><\/pre>\n<\/li>\n<li>Using &#8220;<strong>sed&#8221; <\/strong>command:\n<pre><code class=\"language-bash\" data-line=\"\">SPLIT_STRING=$(echo &quot;$STRING_VARIABLE&quot; | sed &#039;s\/DELIMITER\/REPLACING_DELIMITER\/g&#039;)<\/code><\/pre>\n<\/li>\n<li>Using &#8220;<strong>cut&#8221; <\/strong>command:\n<pre><code class=\"language-bash\" data-line=\"\">echo &quot;$STRING_VARIABLE&quot; | cut -d DELIMITER -f FIELD_NUMBER<\/code><\/pre>\n<\/li>\n<\/ol>\n<p style=\"text-align: justify;\">This article will walk you through these 8 methods to split strings in Bash.<\/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\/split-string\/#1_Using_IFS_Variable\" >1. Using IFS Variable<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#2_Using_%E2%80%9Creadarrray%E2%80%9D_Command\" >2. Using \u201creadarrray\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#3_Using_Parameter_Expansion\" >3. Using Parameter Expansion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#4_Using_Positional_Parameter\" >4. Using Positional Parameter<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#5_Using_%E2%80%9Ctr%E2%80%9D_Command\" >5. Using \u201ctr\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#6_Using_%E2%80%9Cawk%E2%80%9D_Command\" >6. Using \u201cawk\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#7_Using_%E2%80%9Csed%E2%80%9D_Command\" >7. Using \u201csed\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#8_Using_%E2%80%9Ccut%E2%80%9D_Command\" >8. Using \u201ccut\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#8_Bash_String_Split_Examples\" >8 Bash String Split Examples<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#1_Split_String_by_Space_into_Array\" >1. Split String by Space into Array<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#2_Bash_Split_String_into_Variable\" >2. Bash Split String into Variable<\/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\/split-string\/#3_Bash_Split_String_by_%E2%80%9C%E2%80%9D\" >3. Bash Split String by \u201c@\u201d<\/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\/split-string\/#4_Bash_Split_String_by_Colon\" >4. Bash Split String by Colon<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#5_Bash_Split_String_by_Comma\" >5. Bash Split String by Comma<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#6_Bash_Split_Multiline_String\" >6. Bash Split Multiline String<\/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\/split-string\/#7_Bash_Split_String_by_a_Substring_or_Multi-character_Delimiter\" >7. Bash Split String by a Substring or Multi-character Delimiter<\/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\/split-string\/#8_Bash_Split_String_by_Newline_Characters\" >8. Bash Split String by Newline Characters<\/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\/split-string\/#Solved_Handling_Empty_Fields\" >Solved: Handling Empty Fields<\/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\/split-string\/#What_is_the_Difference_Between_Split_and_Tokenize\" >What is the Difference Between Split and Tokenize?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#Best_Practices_to_Split_String\" >Best Practices to Split String<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#Practice_Tasks_on_Bash_Split_String\" >Practice Tasks on Bash Split String<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#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-24\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#Why_its_necessary_to_split_a_string\" >Why it\u2019s necessary to split a string?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-25\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#What_happens_if_the_delimiter_is_not_found_in_the_string\" >What happens if the delimiter is not found in the string?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-26\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#What_is_the_difference_between_%E2%80%98variablepattern_and_%E2%80%98variablepattern_in_string_splitting\" >What is the difference between &#8216;${variable%%pattern}&#8217; and &#8216;${variable#pattern}&#8217; in string splitting?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-27\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#Can_I_split_a_string_into_characters_in_Bash\" >Can I split a string into characters in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-28\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#How_do_I_split_a_string_in_Bash\" >How do I split 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-29\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#How_do_you_split_as_a_string_in_Bash_without_IFS\" >How do you split as a string in Bash without IFS?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"1_Using_IFS_Variable\"><\/span>1. Using IFS Variable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\"><strong>IFS <\/strong>(Internal Field Separator) is a special shell variable used to split the string based on the assigned delimiter. Any character or value (<code class=\"\" data-line=\"\">\\n<\/code>, <code class=\"\" data-line=\"\">-<\/code> ,etc) can be the delimiter. IFS is assigned with whitespace characters by default. Nevertheless, it\u2019s customizable, you can set a different set of characters as delimiters like space, tab, and new line.<\/p>\n<p>Here\u2019s an example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash \n\nstring=&quot;Learn-to-split-string-with-LinuxSipmply&quot; \n\n#Set dash as delimiter \nIFS=&#039;-&#039; read -a array &lt;&lt;&lt; &quot;$string&quot; \n\n#Print the split string using the loop \nfor word in &quot;${array[@]}&quot;; do \necho &quot;$word&quot; \ndone<\/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 script initializes a string variable and then assigns\u00a0<strong>IFS\u00a0<\/strong> variable with the\u00a0<strong>dash\u00a0<\/strong><code class=\"\" data-line=\"\">-<\/code> as the delimiter. In the snippet <code class=\"\" data-line=\"\">IFS=&#039;-&#039; read - a array &lt;&lt;&lt; &quot;$string&quot;<\/code>, the <strong>read command<\/strong> reads the <strong>string <\/strong>variable and separates it into individual words using the\u00a0<strong>dash<\/strong> as the delimiter, and then stores those words into the <strong>array.<\/strong>\u00a0Here, the <code class=\"\" data-line=\"\">-a<\/code> option tells the \u201cread\u201d command to read the input line into the array. After splitting, the <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/\" target=\"_blank\" rel=\"noopener\"><strong>for loop<\/strong><\/a> iterates through each element of the <strong>array <\/strong>and prints them.<\/div><\/div><strong><br \/>\n<\/strong><br \/>\n<img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-34466\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/1-IFS.png\" alt=\"Split string using IFS variable\" width=\"496\" height=\"226\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/1-IFS.png 496w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/1-IFS-300x137.png 300w\" sizes=\"(max-width: 496px) 100vw, 496px\" \/> Here, the script splits the <strong>string <\/strong>according to the <strong>IFS <\/strong>variable where the delimiter is a dash <strong>&#8220;<\/strong><strong>-&#8220;<\/strong>.<\/p>\n<div class=\"su-note\"  style=\"border-color:#cedfe2;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#e8f9fc;border-color:#ffffff;color:#333333;\">\n<p style=\"text-align: justify;\"><strong>Note:\u00a0 <\/strong>You can assign <strong>IFS <\/strong>with any particular character to split the string according to that character or symbol.<\/div><\/div>\n<h2><span class=\"ez-toc-section\" id=\"2_Using_%E2%80%9Creadarrray%E2%80%9D_Command\"><\/span>2. Using \u201creadarrray\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The <strong>readarray <\/strong>command in Bash reads lines from standard user input (such as a string or a file) and stores the elements in an array. To split a string, use the\u00a0<strong>readarray<\/strong> command along with the option <code class=\"\" data-line=\"\">-d<\/code> to specify the delimiter like the syntax\u00a0<code class=\"\" data-line=\"\">readarray -d &quot;delimiter&quot; array_name &lt;&lt;&lt; &quot;$string_variable&quot;<\/code>.<\/p>\n<p style=\"text-align: justify;\">Check\u00a0 the following script to split the string using \u201creadarray\u201d command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash \n\nstring=&quot;Learn-to-split-string-with-LinuxSipmply&quot; \n\n#split the string with readarray command \nreadarray -d &quot;-&quot; -t array &lt;&lt;&lt; &quot;$string&quot; \n\n#print the array \nfor word in &quot;${array[@]}&quot;; \ndo \n printf &quot;%s\\n&quot; &quot;$word&quot; \ndone<\/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\">Here, the <code class=\"\" data-line=\"\">readarray<\/code> command reads the <strong>string<\/strong> into the <strong>array<\/strong>. The <code class=\"\" data-line=\"\">-d &quot;-&quot;<\/code> option specifies the dash <code class=\"\" data-line=\"\">-<\/code> as the delimiter, and<code class=\"\" data-line=\"\"> -t <\/code>trims trailing newline characters. The <code class=\"\" data-line=\"\">printf &quot;%s\\n&quot; &quot;&quot;$word&quot;<\/code>\u00a0 prints each element of the array <strong>word<\/strong> in a newline.<\/div><\/div>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-34467\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/2-readarray.png\" alt=\"Split string using &quot;readarray&quot; command. \" width=\"563\" height=\"237\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/2-readarray.png 563w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/2-readarray-300x126.png 300w\" sizes=\"(max-width: 563px) 100vw, 563px\" \/><\/p>\n<p style=\"text-align: justify;\">As you run the script, the resulting output displays the split-ted string into words based on the dash <code class=\"\" data-line=\"\">-<\/code> delimiter.<\/p>\n<div class=\"su-note\"  style=\"border-color:#cedfe2;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#e8f9fc;border-color:#ffffff;color:#333333;\"><strong>Note:<\/strong>\u00a0 You can assign any particular character after the <code class=\"\" data-line=\"\">-d<\/code> option to split the string according to that character or symbol.<\/div><\/div>\n<h2><span class=\"ez-toc-section\" id=\"3_Using_Parameter_Expansion\"><\/span>3. Using Parameter Expansion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/parameter-expansion\/\" target=\"_blank\" rel=\"noopener\"><strong>Parameter expansion<\/strong><\/a> is the manipulation tool in Bash that finds, replaces, or modifies the parameter values. In Bash, to split a string, use the parameter expansion expressions such as <code class=\"\" data-line=\"\">${string#*delimiter}<\/code> and <code class=\"\" data-line=\"\">${string##*delimiter}<\/code> to eliminate a prefix from a string. Additionally, <code class=\"\" data-line=\"\">${string%delimiter*}<\/code> and <code class=\"\" data-line=\"\">${string%%delimiter*}<\/code> are employed to remove a suffix.<\/p>\n<p>Here\u2019s an example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash \n\nstring=&#039;Bash@Split@String@Parameter@Expansion&#039; \n\n#Remove the shortest match of substring from the beginning\necho &quot;Splits the string at the shortest match of *@ from starting&quot;\necho &quot;${string#*@}&quot;\necho\n\n#Remove the longest match of substring from the beginning\necho &quot;Splits the string at the longest match of *@ from starting&quot;\necho &quot;${string##*@}&quot; \necho\n\n#Remove the shortest match of substring from the end\necho &quot;Splits the string at the shortest match of @* from the end&quot;\necho &quot;${string%@*}&quot; \necho\n\n#Remove the longest match of substring from the end\necho &quot;Splits the string at the longest match of @* from the end&quot;\necho &quot;${string%%@*}&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;\">Here,<code class=\"\" data-line=\"\"> ${string#*:}<\/code> removes the shortest match of <code class=\"\" data-line=\"\">*:<\/code> (anything followed by a colon) from the beginning of the string. It removes &#8220;Bash:&#8221; and gives <code class=\"\" data-line=\"\">&#039;Split:String:Parameter:Expansion&#039;<\/code>.<\/p>\n<p style=\"text-align: justify;\"><code class=\"\" data-line=\"\">${string##*:}<\/code> removes the longest match of <code class=\"\" data-line=\"\">*:<\/code> from the beginning of the string. It removes everything up to the last colon and gives &#8220;Expansion&#8221;.<\/p>\n<p style=\"text-align: justify;\"><code class=\"\" data-line=\"\">${string%:*}<\/code> removes the shortest match of <code class=\"\" data-line=\"\">:*<\/code>\u00a0 from the end of the string. It removes &#8220;:Expansion&#8221; and gives &#8220;Bash:Split:String:Parameter&#8221;.<\/p>\n<p style=\"text-align: justify;\"><code class=\"\" data-line=\"\">${string%%:*}<\/code> removes the longest match of <code class=\"\" data-line=\"\">*:<\/code> from the end of the string. It removes everything up to the first colon and gives &#8220;Bash&#8221;.<\/div><\/div>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-39874\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/3-parameter-expansion-new.png\" alt=\"Bash split string using parameter expansion.\" width=\"825\" height=\"331\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/3-parameter-expansion-new.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/3-parameter-expansion-new-300x120.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/3-parameter-expansion-new-768x308.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The script splits the string in four different ways according to the parameter expansion expression.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"4_Using_Positional_Parameter\"><\/span>4. Using Positional Parameter<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In Bash, <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/parameters\/positional-parameters\/\" target=\"_blank\" rel=\"noopener\"><strong>positional parameters<\/strong><\/a> are special variables that automatically store the arguments passed to a script or function. These variables are sequential numbers (like 1,2 3..) and they can be accessed using the dollar sign symbol <code class=\"\" data-line=\"\">$<\/code> followed by the number. To set positional parameters, the &#8220;set&#8221; command is used.<\/p>\n<p style=\"text-align: justify;\">To split a string using the positional parameter with the \u201cset\u201d command, you can follow the script below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&quot;Split string using positional parameter&quot;\n\n# Set positional parameters to the words in the string\nset -- $string\n\n# Iterate through positional parameters using a loop\nfor word in $@; do\necho &quot;$word&quot;\ndone<\/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=\"\">set -- $string<\/code> statement sets the positional parameters (<strong>$1, $2, $3,<\/strong> etc.) to each word of the <strong>string<\/strong>. The <strong>double dash <\/strong><code class=\"\" data-line=\"\">--<\/code>\u00a0is used to prevent ambiguity in case the <strong>$string <\/strong>begins with a <strong>dash <\/strong><code class=\"\" data-line=\"\">-<\/code>, which might be misinterpreted as an option by the <strong>set <\/strong>command.<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34469\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/4-positional-parameter.png\" alt=\"The output of split string operation using position parameter. \" width=\"663\" height=\"195\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/4-positional-parameter.png 663w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/4-positional-parameter-300x88.png 300w\" sizes=\"(max-width: 663px) 100vw, 663px\" \/> Here, the string is split on each positional value.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"5_Using_%E2%80%9Ctr%E2%80%9D_Command\"><\/span>5. Using \u201ctr\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The <a href=\"https:\/\/linuxsimply.com\/tr-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>tr command <\/strong><\/a>manipulates text data by translating or removing characters to perform transformation operations. To split a string using<strong> \u201ctr\u201d,<\/strong> use the below bash script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Input string\nstring=&quot;Ubuntu, RHEL, Fedora, Kali, CentOS&quot;\n\n# Use tr command to replace the delimiter character with a newline\narray=($(echo $string | tr &quot;,&quot; &quot;\\n&quot;))\n\n# Print element of the array\nfor element in &quot;${array[@]}&quot;; do\necho &quot;$element&quot;\ndone<\/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=\"\">echo $string | tr &quot;,&quot; &quot;\\n&quot;<\/code> uses the echo command and pipes <code class=\"\" data-line=\"\">|<\/code> the string to the <strong>tr command<\/strong>. The <strong>tr<\/strong> command is used to replace every occurrence of the <strong>comma <\/strong><code class=\"\" data-line=\"\">,<\/code>\u00a0with a <strong>newline character <\/strong><code class=\"\" data-line=\"\">\\n<\/code>. This effectively transforms the string into multiple lines.<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34470\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/5-tr-command.png\" alt=\"The output of split string using &quot;tr&quot; command\" width=\"533\" height=\"200\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/5-tr-command.png 533w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/5-tr-command-300x113.png 300w\" sizes=\"(max-width: 533px) 100vw, 533px\" \/>The script splits the string by replacing the comma (<code class=\"\" data-line=\"\">,<\/code>) of the string with the newline character <code class=\"\" data-line=\"\">\\n<\/code>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"6_Using_%E2%80%9Cawk%E2%80%9D_Command\"><\/span>6. Using \u201cawk\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\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 a text-processing tool that is suitable for pattern matching, extracting, and data manipulation from files or streams. To split a string, you can use the \u201cawk\u201d command with the <code class=\"\" data-line=\"\">-F<\/code>\u00a0option which sets the file separator value. You can check the following example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&quot;ubuntu:fedora:rhel:centos&quot;\n\necho &quot;$string&quot; | awk -F &#039;:&#039; &#039;{print $1,$2,$3}&#039;<\/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 class=\"\" data-line=\"\">echo string<\/code> has been piped to<strong> the awk <\/strong>command. The option <code class=\"\" data-line=\"\">-F<\/code> sets the colon <code class=\"\" data-line=\"\">:<\/code> as the field separator. Subsequently, the \u201cawk\u201d command splits the input string into fields based on the colon. <code class=\"\" data-line=\"\">{print $1,$2,$3}<\/code> refers to the script that will print the first three fields of the string separated by the colon. <\/div><\/div>\n<p style=\"text-align: justify;\"><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34471\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/6-awk-command-1.png\" alt=\"The output of split string operation using &quot;awk&quot; command.\" width=\"460\" height=\"68\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/6-awk-command-1.png 460w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/6-awk-command-1-300x44.png 300w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/strong>The script splits the string and displays output (<strong>ubuntu fedora rhel<\/strong>) as shown in the picture<strong>. <\/strong><\/p>\n<h2><span class=\"ez-toc-section\" id=\"7_Using_%E2%80%9Csed%E2%80%9D_Command\"><\/span>7. Using \u201csed\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The <strong><a href=\"https:\/\/linuxsimply.com\/sed-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">sed command<\/a><\/strong> is a powerful tool used for text stream processing. It performs operations on text like search, replace, insert, and delete operations. This command can be used to split strings with the <code class=\"\" data-line=\"\">-s<\/code> option.<\/p>\n<p style=\"text-align: justify;\">To split a string using the &#8220;sed&#8221; command in Bash, you can use the following script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&quot;Bash,Loop,String,Split&quot;\n\necho $string | sed &#039;s\/,\/\\\n\/g&#039;<\/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, the <code class=\"\" data-line=\"\">sed<\/code> command to substitute every comma <code class=\"\" data-line=\"\">,<\/code> with a new line throughout the string, effectively splitting the string into separate lines.<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34472\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/7-sed-command.png\" alt=\"The output of split string operation using &quot;sed&quot; command.\" width=\"509\" height=\"164\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/7-sed-command.png 509w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/7-sed-command-300x97.png 300w\" sizes=\"(max-width: 509px) 100vw, 509px\" \/>This script splits the string by replacing every comma (,) using the &#8220;sed&#8221; command.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"8_Using_%E2%80%9Ccut%E2%80%9D_Command\"><\/span>8. Using \u201ccut\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The <a href=\"https:\/\/linuxsimply.com\/cut-command-in-linux\/\"><strong>cut command <\/strong><\/a>extracts or cuts specific parts of input information. To split a string in Bash, use the<strong> cut command <\/strong>with the option <code class=\"\" data-line=\"\">-d<\/code> to specify the separator. check the following script to split a string using the &#8220;cut&#8221; command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&quot;Bash,Loop,String,Split&quot;\n\necho &quot;$string&quot; | cut -d &#039;,&#039; -f1\necho &quot;$string&quot; | cut -d &#039;,&#039; -f2\necho &quot;$string&quot; | cut -d &#039;,&#039; -f3\necho &quot;$string&quot; | cut -d &#039;,&#039; -f4<\/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\">This command uses the &#8220;cut&#8221; command to split the input string, which contains names separated by commas. The <code class=\"\" data-line=\"\">-d &#039;,&#039;<\/code> specifies the delimiter as a comma, and <code class=\"\" data-line=\"\">-f1<\/code>,<code class=\"\" data-line=\"\"> -f2<\/code>, <code class=\"\" data-line=\"\">-f3<\/code>, <code class=\"\" data-line=\"\">-f4<\/code> indicates the fields (parts) to be extracted. The output will display each part on a separate line.<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34473\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/8-cut-command.png\" alt=\"The output of split string operation using &quot;cut&quot; command.\" width=\"492\" height=\"163\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/8-cut-command.png 492w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/8-cut-command-300x99.png 300w\" sizes=\"(max-width: 492px) 100vw, 492px\" \/>The output shows the split-ted string using the <strong>cut <\/strong>command after running the script.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"8_Bash_String_Split_Examples\"><\/span>8 Bash String Split Examples<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">This section states 8 Bash split string examples based on various delimiters like space, colon, comma, substring, etc. You can excel in your skill in splitting strings in Bash by interacting with the examples stated below:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Split_String_by_Space_into_Array\"><\/span>1. Split String by Space into Array<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">You can split a string into small segments and store them in an <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/\" target=\"_blank\" rel=\"noopener\"><strong>array<\/strong><\/a> using &#8220;readarray&#8221; command followed by the <code class=\"\" data-line=\"\">-d<\/code> option and an array. Check the below script to get an example of splitting a string into an array:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Input String\nstring=&quot;Split the string into Array&quot;\n\n#Split the string by space\nreadarray -d &quot; &quot; -t array &lt;&lt;&lt; &quot;$string&quot;\n\n#print the array\ni=0\nfor element in &quot;${array[@]}&quot;; do\necho &quot;Array[$i]: $element&quot;\ni=$((i+1))\ndone<\/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, the <strong>string<\/strong> variable is assigned with the string \u201cSplit the string into Array\u201d.\u00a0The syntax for this operation is: <code class=\"\" data-line=\"\">read -a array &lt;&lt;&lt; &quot;$string&quot;<\/code>. This command effectively reads a line from the standard input <strong>string<\/strong>, divides it into distinct fields based on <strong>space<\/strong>, and then assigns those fields to an array named &#8220;array&#8221;. At last, the <strong>array <\/strong>is printed using a for loop.<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34474\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/9-Example1_split-string-into-array.png\" alt=\"Splitting string into array.\" width=\"553\" height=\"205\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/9-Example1_split-string-into-array.png 553w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/9-Example1_split-string-into-array-300x111.png 300w\" sizes=\"(max-width: 553px) 100vw, 553px\" \/> Here, you can see all the elements of the array that are split into it.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_Bash_Split_String_into_Variable\"><\/span>2. Bash Split String into Variable<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>You can split a string and store the elements into variables in multiple ways. You can check the approaches from below:<\/p>\n<p><span style=\"color: #008000;\"><strong>2.1 Using\u00a0IFS VAriable:<\/strong><\/span> To split a string and store the split-ted word in multiple variables, check the script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\npkg=&quot;apt,yum,pacman&quot;\n\nIFS=\u2019,\u2019\nread Var1 Var2 Var3 &lt;&lt;&lt;$pkg\n\n#Print the variable\necho &#039;Value of Var1:&#039;$Var1\necho &#039;Value of Var2:&#039;$Var2\necho &#039;Value of Var3:&#039;$Var3<\/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 script assigns <strong>IFS <\/strong> variable with <strong>comma <\/strong><code class=\"\" data-line=\"\">,<\/code>. Then, the <strong>read <\/strong>command takes the string\u00a0<code class=\"\" data-line=\"\">pkg<\/code> and\u00a0stores them into three separate variables <strong>var1, var2, <\/strong>and<strong> var3 <\/strong> after splitting.<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34475\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/10-Example2_1_split-string-into-var.png\" alt=\"Bash split string into variable using &quot;read&quot; command.\" width=\"573\" height=\"141\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/10-Example2_1_split-string-into-var.png 573w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/10-Example2_1_split-string-into-var-300x74.png 300w\" sizes=\"(max-width: 573px) 100vw, 573px\" \/>The script output shows each variable&#8217;s values after splitting the string.<\/p>\n<p style=\"text-align: justify;\">Here&#8217;s another example,<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">IFS=- read v1 v2 v3 v4 &lt;&lt;&lt; Split-string-into-variables\n\necho &quot;$v1 $v2 $v3 $v4&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-39883\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/example-22-split-stirng-into-variable.png\" alt=\"the output of split string using &quot;IFS&quot; variable.\" width=\"825\" height=\"95\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/example-22-split-stirng-into-variable.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/example-22-split-stirng-into-variable-300x35.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/example-22-split-stirng-into-variable-768x88.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>Here, the <strong>dash <\/strong><code class=\"\" data-line=\"\">-<\/code> separated string has been split into four separate variables (<strong>v1, v2, v3, v4) <\/strong>using the <strong>IFS <\/strong>variable and <strong>read <\/strong>command.<\/p>\n<p><span style=\"color: #008000;\"><strong>2.2 Using Cut Command:<\/strong><\/span> To split a string into variables, use the <strong>cut <\/strong>command as below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">string=&quot;Linux-Ubuntu-Red hat&quot;\n\nv1=$(echo $string | cut -f1 -d -)\nv2=$(echo $string | cut -f2 -d -)\nv3=$(echo $string | cut -f3 -d -)\n\necho $v1\necho $v2\necho $v3<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34477\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12-Example2_3-Split-string-into-variable.png\" alt=\"Bash split string into variable using &quot;cut&quot; command.\" width=\"582\" height=\"137\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12-Example2_3-Split-string-into-variable.png 582w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12-Example2_3-Split-string-into-variable-300x71.png 300w\" sizes=\"(max-width: 582px) 100vw, 582px\" \/>The string has been cut into three variables <strong>(v1, v2, v3) <\/strong>\u00a0based on the<strong> dash <\/strong><code class=\"\" data-line=\"\">-<\/code> delimiter. The output shows the variables after splitting.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Bash_Split_String_by_%E2%80%9C%E2%80%9D\"><\/span>3. Bash Split String by \u201c@\u201d<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To split a string by setting <code class=\"\" data-line=\"\">@<\/code> symbol in the <strong>IFS <\/strong>variable, check the following script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&quot;Split@the@string@using@symbol&quot;\n\n#Set space as delimiter\nIFS=&#039;@&#039;\nread -ra splitstr &lt;&lt;&lt; &quot;$string&quot;\n\n#Print the split string using the loop\nfor word in &quot;${splitstr[@]}&quot;;\ndo\necho &quot;$word&quot;\ndone<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34478\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/13-Example3_split-strinf-using-a-symbol.png\" alt=\"Bash split string using &quot;@&quot;.\" width=\"528\" height=\"202\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/13-Example3_split-strinf-using-a-symbol.png 528w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/13-Example3_split-strinf-using-a-symbol-300x115.png 300w\" sizes=\"(max-width: 528px) 100vw, 528px\" \/>Upon running the script, you\u2019ll get the split-ted string into words as shown in the image.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_Bash_Split_String_by_Colon\"><\/span>4. Bash Split String by Colon<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To split a string using a colon, set the colon <code class=\"\" data-line=\"\">:<\/code> as a field separator within the <strong>IFS <\/strong>variable and use the \u201cread\u201d command with the option <code class=\"\" data-line=\"\">-ra<\/code> to split the string as before:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&quot;Split:the:string:using:symbol&quot;\n\n#Set space as delimiter\nIFS=&#039;:&#039;\nread -ra array &lt;&lt;&lt; &quot;$string&quot;<\/code><\/pre>\n<p>To split the string by colon using the &#8220;readarray&#8221; command, use the following expression after declaring the <strong>string\u00a0<\/strong>variable:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">readarray -d : -t array &lt;&lt;&lt; &quot;$string&quot;<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"5_Bash_Split_String_by_Comma\"><\/span>5. Bash Split String by Comma<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Set field separator as <strong>comma (\u2018,\u2019)<\/strong> using either <strong>IFS <\/strong>variable or &#8220;readarray&#8221; command and split the string as below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">IFS=&#039;,&#039;\nread -ra array &lt;&lt;&lt; &quot;$string&quot;<\/code><\/pre>\n<ul>\n<li style=\"text-align: justify;\">Alternatively, you can use the following expression:<\/li>\n<\/ul>\n<pre><code class=\"language-bash\" data-line=\"\"># Split the string based on the delimiter, &#039;,&#039;\nreadarray -d , -t array &lt;&lt;&lt; &quot;$string&quot;<\/code><\/pre>\n<ul>\n<li style=\"text-align: justify;\">If you want to split a string based on multiple separators or delimiters, set IFS to a custom character class containing multiple separators as below script:<\/li>\n<\/ul>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&quot;Split:This-String,With_Multiple;Separators&quot;\n\n# Set IFS to a custom character class containing multiple separators\nIFS=&#039;[:,;_-]&#039;\n\n# Read the string into an array\nread -ra array &lt;&lt;&lt; &quot;$string&quot;\n\n# Print each element of the array\nfor element in &quot;${array[@]}&quot;; do\necho &quot;$element&quot;\ndone<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34479\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/14-Example5_split-string-using-multiple-delimiter.png\" alt=\"Bash split string by comma.\" width=\"501\" height=\"228\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/14-Example5_split-string-using-multiple-delimiter.png 501w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/14-Example5_split-string-using-multiple-delimiter-300x137.png 300w\" sizes=\"(max-width: 501px) 100vw, 501px\" \/>Here, you can see that, the script has split-ted the string based on the five delimiters (<strong>colon, comma, semicolon, underscore, dash\u00a0 <\/strong>) specified within the <strong>IFS <\/strong>variable.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"6_Bash_Split_Multiline_String\"><\/span>6. Bash Split Multiline String<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Multiline strings can be split into substrings using various approaches such as using parameter expansion, <strong>readarray<\/strong> command, and IFS variable. Here\u2019s how:<\/p>\n<p><span style=\"font-size: 14pt; color: #008000;\"><strong>6.1. Using Parameter Expansion:<\/strong><\/span> You can use parameter expansion to split a multiline string as in the below script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=$&#039;Multiline string is split now.\nEach line is stored as a substring.\nNow print the substring.\nHere\u2019s it.&#039;\n\nsubstring=(&quot;${string\/\/$&#039;\\n&#039;\/ }&quot;)\n\nfor word in &quot;${substring[@]}&quot;; do\necho &quot;$word&quot;\ndone<\/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, <strong>string<\/strong> is assigned with a multiline string.<code class=\"\" data-line=\"\"> substring=(&quot;${string\/\/$&#039;\\n&#039;\/ }&quot;)<\/code> utilizes parameter expansion to replace newline characters <code class=\"\" data-line=\"\">$&#039;\\n&#039;<\/code> with <strong>spaces<\/strong>, effectively transforming the multiline string into a space-separated string. The resulting substrings are assigned to an array named <strong>substring.<\/strong><\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34480\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/15-Example6-spliot-string-using-multiline-character.png\" alt=\"Bash split multiline string using parameter expansion.\" width=\"659\" height=\"134\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/15-Example6-spliot-string-using-multiline-character.png 659w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/15-Example6-spliot-string-using-multiline-character-300x61.png 300w\" sizes=\"(max-width: 659px) 100vw, 659px\" \/>As you run the script, you\u2019ll see the <strong>newline character<\/strong> from the multiline string has been replaced with <strong>space<\/strong>. That\u2019s why the output shows the string as a paragraph.<\/p>\n<p><span style=\"color: #008000; font-size: 14pt;\"><strong>6.2 Using &#8220;readarray&#8221; Command:<\/strong><\/span> To split the multiline string using &#8220;readarray&#8221; command, follow the script below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nstring=$&#039;Multiline string is split now.\nEach line is stored as a substring.\nNow print the substring.\nHere\u2019s it.&#039;\n\nreadarray -t substring &lt;&lt;&lt;&quot;$string&quot;\n\n#Print the substrings\ni=1\nfor word in &quot;${substring[@]}&quot;; do\necho &quot;Subtring $i: $word&quot;\n((i=i+1))\ndone<\/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, the &#8220;readarray&#8221; command reads from the <strong>string<\/strong> variable into the <strong>substring<\/strong> array. The <code class=\"\" data-line=\"\">-t<\/code> option removes the trailing newline (\u2018\\n\u2019) characters.<\/div><\/div> <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34481\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/16Example-6_1_split-string-using-multiline-string.png\" alt=\"Bash split multiline string using 'readarray&quot; command.\" width=\"700\" height=\"149\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/16Example-6_1_split-string-using-multiline-string.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/16Example-6_1_split-string-using-multiline-string-300x64.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p style=\"text-align: justify;\">The script splits the multiline string into separate substrings considering the newline character.<\/p>\n<p><span style=\"font-size: 14pt; color: #008000;\"><strong>6.3 Using\u00a0IFS Variable:\u00a0<\/strong><\/span> If you want to split the multiline string using the <strong>IFS <\/strong>variable, replace the <code class=\"\" data-line=\"\">readarray -t substring &lt;&lt;&lt;&quot;$string&quot;<\/code> line of the above script with the following line:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">IFS=$&#039;\\n&#039; read -rd &#039;&#039; -a substring &lt;&lt;&lt;&quot;$string&quot;<\/code><\/pre>\n<p><span style=\"color: #008000; font-size: 14pt;\"><strong>6.4 Using IFS Variable and Parameter Expansion:<\/strong> <\/span>You can combine the <strong>IFS <\/strong>variable and <strong>parameter expansion<\/strong> to split a multiline string in Bash. To do that, use the following line instead of the previous one:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">IFS=$&#039;\\n&#039; substring=(${string\/\/$&#039;\\n&#039;\/ })<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"7_Bash_Split_String_by_a_Substring_or_Multi-character_Delimiter\"><\/span>7. Bash Split String by a Substring or Multi-character Delimiter<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <strong>multi-character delimiter<\/strong> is a sequence of characters that separates different segments of a string or text. To split a string using a multi-character delimiter, follow the script below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Read the main string\nstring1=&quot;distroUbuntu distroRHEL distroKali distroFedora &quot;\n\n#Define the multi-character delimiter\ndelimiter=&quot;distro&quot;\n\n#concatenate the delimiter with the string\nstring=$string1$delimiter\n\n#split the string based on the delimiter\narray=()\nwhile [[ $string ]]; do\narray+=( &quot;${string%%$delimiter*}&quot; )\nstring=${string#*$delimiter}\ndone\n\n#print the elements of the array\nfor val in &quot;${array[@]}&quot;\ndo\necho -n &quot;$val&quot;\ndone<\/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;\"><code class=\"\" data-line=\"\">string=$string1$delimiter<\/code> appends the delimiter with the <strong>string1. <\/strong>Then, to split the string iteratively, <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/\" target=\"_blank\" rel=\"noopener\"><strong>while loop<\/strong><\/a> is used. Here, <code class=\"\" data-line=\"\">array+=( &quot;${string%%$delimiter*}&quot; )<\/code> removes the longest suffix pattern matching <code class=\"\" data-line=\"\">$delimiter*<\/code> from the end of the <strong>string<\/strong> and appends each split element to the array. Whereas, <code class=\"\" data-line=\"\">string=${string#*$delimiter}<\/code> removes the shortest prefix pattern matching <code class=\"\" data-line=\"\">*$delimiter<\/code> from the beginning of the string. The loop continues until the string variable becomes empty. The loop continues until the string variable becomes empty, at which point the splitting is complete. <\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-34482\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/17-Example-7-split-array-using-substring.png\" alt=\"Bash split string by a substring.\" width=\"580\" height=\"107\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/17-Example-7-split-array-using-substring.png 580w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/17-Example-7-split-array-using-substring-300x55.png 300w\" sizes=\"(max-width: 580px) 100vw, 580px\" \/>The output shows the split-ted string based on the substring \u201c<strong>distro<\/strong>\u201d.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"8_Bash_Split_String_by_Newline_Characters\"><\/span>8. Bash Split String by Newline Characters<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>You can use the <strong>IFS<\/strong> variable to split a string based on the <strong>newline<\/strong> (\u2018\\n\u2019) character as below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">x=&#039;some\nthing&#039;\ny=${x%$&#039;\\n&#039;*}<\/code><\/pre>\n<p>After running the script, you can see the string is split into the array.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Solved_Handling_Empty_Fields\"><\/span>Solved: Handling Empty Fields<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">A common problem while splitting a string into an array in Bash is dealing with empty fields. If your string has <strong>consecutive separators<\/strong>, Bash may consider them as <strong>empty fields<\/strong>. Let me illustrate this with an example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&#039;Bash::String::Splitting&#039;\n\nIFS=&#039;:&#039; read -a array &lt;&lt;&lt; &quot;$string&quot;\n\ni=0\nfor element in &quot;${array[@]}&quot;; do\necho &quot;array[$i]&quot;:&quot;$element&quot;\ni=$((i+1))\ndone\n\n#Output\n#array[0]:Bash\n#array[1]:\n#array[2]:String\n#array[3]:\n#array[4]:Splitting<\/code><\/pre>\n<p style=\"text-align: justify;\">Here you can see from the output that the elements of \u2018array[1]\u2019 and \u2018array[3]\u2019 are empty fields. Bash interprets consecutive colons as an empty field here.<\/p>\n<p style=\"text-align: justify;\">To solve this, <strong>tokenize the string<\/strong> by using a <strong>loop<\/strong> to iterate over the array elements and print them only if they are<strong> not empty<\/strong>. Follow the script below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&#039;Bash::Scripting&#039;\n\nIFS=&#039;:&#039; read -a array &lt;&lt;&lt; &quot;$string&quot;\n\n# Print non-empty elements of the array\nfor element in &quot;${array[@]}&quot;; do\nif [[ -n &quot;$element&quot; ]]; then\necho &quot;$element&quot;\nfi\ndone<\/code><\/pre>\n<p style=\"text-align: justify;\">Here, the condition <code class=\"\" data-line=\"\">-n &quot;$element&quot;<\/code> within the if statement checks if the element is not an empty string. If an element is not empty, it gets printed.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_is_the_Difference_Between_Split_and_Tokenize\"><\/span>What is the Difference Between Split and Tokenize?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">While &#8220;splitting&#8221; generally means dividing a string into parts based on a specific criterion (like a delimiter), &#8220;tokenizing&#8221; has a broader meaning and often involves breaking a text into meaningful units (tokens) based on certain rules or patterns.\u00a0 For example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#bash split string example\nsentence=&quot;Alice, Bob, and Charlie&quot;\n\nIFS=&#039;, &#039; read -ra split_result &lt;&lt;&lt; &quot;$sentence&quot;\n\nfor part in &quot;${split_result[@]}&quot;; do\necho &quot;$part&quot;\ndone\n\n#output\nAlice\nBob\nand Charlie<\/code><\/pre>\n<p style=\"text-align: justify;\">Let&#8217;s have an example of tokenizing below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#tokenizing example\nsentence=&quot;Tokenization and splitting are almost same.&quot;\necho &quot;$sentence&quot; | awk &#039;BEGIN {RS=&quot;[^A-Za-z0-9]+&quot;} {print $0}&#039;\n\n#output\nTokenization and splitting are almost the same<\/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, The <strong>BEGIN<\/strong> block is executed before processing any input lines, setting up the<strong> record separator (RS)<\/strong> to define what constitutes a &#8220;word&#8221; in this context. The main block <strong>{print $0} <\/strong>prints each &#8220;word&#8221; on a new line.<\/div><\/div>\n<p style=\"text-align: justify;\">Tokenizing involves breaking a string into meaningful words or phrases, whereas splitting a string refers to breaking it based on a specified delimiter.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Best_Practices_to_Split_String\"><\/span>Best Practices to Split String<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">When you\u2019re splitting strings in Bash, here are some important\u00a0 conventions to keep in mind:<\/p>\n<ol>\n<li style=\"text-align: justify;\"><strong>Quote Variables: <\/strong>Always <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/quotes\/\" target=\"_blank\" rel=\"noopener\"><strong>quote<\/strong><\/a> your variable to prevent word splitting and pathname expansion. This ensures that the string is treated as a single entity, especially when it contains spaces or special characters.<\/li>\n<li style=\"text-align: justify;\"><strong>Save and Restore IFS: <\/strong>If you&#8217;re changing the way strings are split (using IFS), save and restore its original setting: This prevents unexpected issues in other parts of your script.<\/li>\n<li style=\"text-align: justify;\"><strong>Test with Different Inputs: <\/strong>Always test your scripts with different inputs to ensure they handle edge cases correctly.<\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"Practice_Tasks_on_Bash_Split_String\"><\/span>Practice Tasks on Bash Split String<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">If you want to excel in your skill in Bash split string, you can try to solve the following problems using Bash split string concepts.<\/p>\n<ol>\n<li style=\"text-align: justify;\">Write a Bash script that takes a comma-separated list of items as an input string and prints each item on a new line.<\/li>\n<li style=\"text-align: justify;\">Split String by Delimiter and Get N-th Element in Bash Shell Script.<\/li>\n<li style=\"text-align: justify;\">Write a bash script that takes a URL as input and extracts the domain name.<\/li>\n<li style=\"text-align: justify;\">Suppose a given file contains IP addresses on each line. Extract those that belong to a specific subnet. For example, extract addresses within the 192.168.1.0\/24.<\/li>\n<li style=\"text-align: justify;\">Create a Bash script that takes a sentence as input and reverses the order of words. The scripts should then print the modified sentence.<\/li>\n<\/ol>\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, splitting a string in Bash is a valuable skill for Linux developers to extract data from log files and process user input, understanding various methods of string manipulation. Hope this article helped you to gain the expertise on Bash string splitting.<\/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=\"Why_its_necessary_to_split_a_string\"><\/span>Why it\u2019s necessary to split a string?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Splitting a string is necessary for various purposes including programming and scripting. Here are some key reasons why it\u2019s necessary:<\/p>\n<ul>\n<li style=\"text-align: justify;\">Data Extraction<\/li>\n<li style=\"text-align: justify;\">Configuration Parsing<\/li>\n<li style=\"text-align: justify;\">Text Processing and Filtering<\/li>\n<li style=\"text-align: justify;\">Path manipulation<\/li>\n<li style=\"text-align: justify;\">User Input handling<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"What_happens_if_the_delimiter_is_not_found_in_the_string\"><\/span>What happens if the delimiter is not found in the string?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">If the delimiter is not found in the string, the splitting operation typically returns the entire string as a single element, as there is no delimiter to separate the string into distinct parts.<\/p>\n<p style=\"text-align: justify;\">To handle leading and trailing whitespace during splitting in Bash, you can follow the script below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&quot; John is\u00a0\u00a0 17\u00a0\u00a0 years\u00a0\u00a0 old\u00a0\u00a0 &quot;\n\n# Set IFS to include space\nIFS=&quot; &quot;\n\n# Tokenize using the read command\nread -r -a tokens &lt;&lt;&lt; &quot;$string&quot;\n\n# Print each token\nfor token in &quot;${tokens[@]}&quot;; do\necho &quot;$token&quot;\ndone<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_difference_between_%E2%80%98variablepattern_and_%E2%80%98variablepattern_in_string_splitting\"><\/span>What is the difference between &#8216;${variable%%pattern}&#8217; and &#8216;${variable#pattern}&#8217; in string splitting?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The syntax<code class=\"\" data-line=\"\">${variable%%pattern}<\/code> removes the longest match from the end, whereas the <code class=\"\" data-line=\"\">${variable#pattern}<\/code> removes the shortest match from the beginning of the variable content based on the specified pattern.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Can_I_split_a_string_into_characters_in_Bash\"><\/span>Can I split a string into characters in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Yes<\/strong>, you can split a string into key-value pairs methods like <strong>parameter expansion<\/strong> or <strong>awk. <\/strong>Ensure that your string follows a consistent pattern. Here\u2019s a simple example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&quot;Hello&quot;\n\n# Split the string into characters\nfor ((i=0; i&lt;${#string}; i++)); do\necho &quot;${string:$i:1}&quot;\ndone<\/code><\/pre>\n<p style=\"text-align: justify;\">Here,<strong> ${#string}<\/strong> gives the length of the string and <strong>${string:$i:1}<\/strong> extracts a substring of length 1, starting from position<strong> $i<\/strong> in the string.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_split_a_string_in_Bash\"><\/span>How do I split a string in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To split a string in Bash, use the <strong>IFS\u00a0<\/strong> variable, <strong>readarray <\/strong>command, or <strong>cut <\/strong>command.\u00a0 For example, to split a string on a delimiter <strong>dash<\/strong>\u00a0<code class=\"\" data-line=\"\">-<\/code>, use the\u00a0<strong>IFS<\/strong> variable to assign the delimiter, and then read the <strong>string<\/strong> into an <strong>array <\/strong>splitting it. In this case, the syntax will be:\u00a0<code class=\"\" data-line=\"\">IFS=&#039;-&#039; read array &lt;&lt;&lt; &quot;&quot;$string<\/code>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_you_split_as_a_string_in_Bash_without_IFS\"><\/span>How do you split as a string in Bash without IFS?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To split a string in Bash without IFS, use the\u00a0<strong>cut <\/strong>command,\u00a0<strong> readarray <\/strong>command,\u00a0<strong>awk <\/strong>command, etc. For example, to split a string on a delimiter\u00a0<strong>comma\u00a0<\/strong><code class=\"\" data-line=\"\">,<\/code> without using an <strong>IFS\u00a0<\/strong>variable, use\u00a0<strong>readarray <\/strong> command as\u00a0the syntax: <code class=\"\" data-line=\"\">readarray -d &quot;,&quot; -t array &lt;&lt;&lt; &quot;$string&quot;<\/code>.<br \/>\n<script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"How do I split a string on a delimiter in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To split a string on a delimiter, use the\u00a0IFS\u00a0 variable, readarray command, or cut command.\u00a0 For example, to split a string on a delimiter dash\u00a0-, use the\u00a0IFS variable to assign the delimiter, and then read the string into an array splitting it. In this case, the syntax will be:\u00a0IFS='-' read array <<< \\\"\\\"$string.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How do you split as a string in Bash without IFS?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To split a string in Bash without IFS, use the\u00a0cut command,\u00a0 readarray command,\u00a0awk command, etc. For example, to split a string on a delimiter\u00a0comma\u00a0, without using an IFS\u00a0variable, use\u00a0readarray command as\u00a0the syntax: readarray -d \\\",\\\" -t array <<< \\\"$string\\\".\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is the difference between '${variable%%pattern}' and '${variable#pattern}' in string splitting?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"The syntax${variable%%pattern} removes the longest match from the end, whereas the ${variable#pattern} removes the shortest match from the beginning of the variable content based on the specified pattern.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"\"\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\/substring\/\" target=\"_blank\" rel=\"noopener\">How to Extract Bash Substring? [5 methods]<\/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;33986&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;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&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;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;8 Methods to Split String in Bash [With Examples]&quot;,&quot;width&quot;:&quot;142.5&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: 142.5px;\">\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            5\/5 - (1 vote)    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bash split string refers to slicing a string into several parts based on a specific symbol, character, or substring as &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"8 Methods to Split String in Bash [With Examples]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#more-33986\" aria-label=\"Read more about 8 Methods to Split String in Bash [With Examples]\">Read more<\/a><\/p>\n","protected":false},"author":314912,"featured_media":34483,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[189],"class_list":["post-33986","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>8 Methods to Split String in Bash [With Examples] - LinuxSimply<\/title>\n<meta name=\"description\" content=\"Bash split string refers to splitting a string into multiple strings or elements based on various separator (delimiter).\" \/>\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\/split-string\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"8 Methods to Split String in Bash [With Examples] - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"Bash split string refers to splitting a string into multiple strings or elements based on various separator (delimiter).\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/\" \/>\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-16T12:05:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-28T04:48:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/18-Bash-Split-String.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=\"17 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\\\/split-string\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/split-string\\\/\"},\"author\":{\"name\":\"Auhona Islam\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/25a3d16db4655804617c9e0c4a7989fb\"},\"headline\":\"8 Methods to Split String in Bash [With Examples]\",\"datePublished\":\"2024-01-16T12:05:52+00:00\",\"dateModified\":\"2024-04-28T04:48:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/split-string\\\/\"},\"wordCount\":3122,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/split-string\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/18-Bash-Split-String.png\",\"keywords\":[\"bash string\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/split-string\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/split-string\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/split-string\\\/\",\"name\":\"8 Methods to Split String in Bash [With Examples] - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/split-string\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/split-string\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/18-Bash-Split-String.png\",\"datePublished\":\"2024-01-16T12:05:52+00:00\",\"dateModified\":\"2024-04-28T04:48:40+00:00\",\"description\":\"Bash split string refers to splitting a string into multiple strings or elements based on various separator (delimiter).\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/split-string\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/split-string\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/split-string\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/18-Bash-Split-String.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/18-Bash-Split-String.png\",\"width\":400,\"height\":400,\"caption\":\"Bash split string\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/split-string\\\/#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\":\"8 Methods to Split String in Bash [With Examples]\"}]},{\"@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":"8 Methods to Split String in Bash [With Examples] - LinuxSimply","description":"Bash split string refers to splitting a string into multiple strings or elements based on various separator (delimiter).","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\/split-string\/","og_locale":"en_US","og_type":"article","og_title":"8 Methods to Split String in Bash [With Examples] - LinuxSimply","og_description":"Bash split string refers to splitting a string into multiple strings or elements based on various separator (delimiter).","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/profile.php?id=100077694755838&mibextid=ZbWKwL","article_published_time":"2024-01-16T12:05:52+00:00","article_modified_time":"2024-04-28T04:48:40+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/18-Bash-Split-String.png","type":"image\/png"}],"author":"Auhona Islam","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Auhona Islam","Est. reading time":"17 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/"},"author":{"name":"Auhona Islam","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/25a3d16db4655804617c9e0c4a7989fb"},"headline":"8 Methods to Split String in Bash [With Examples]","datePublished":"2024-01-16T12:05:52+00:00","dateModified":"2024-04-28T04:48:40+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/"},"wordCount":3122,"commentCount":2,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/18-Bash-Split-String.png","keywords":["bash string"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/","name":"8 Methods to Split String in Bash [With Examples] - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/18-Bash-Split-String.png","datePublished":"2024-01-16T12:05:52+00:00","dateModified":"2024-04-28T04:48:40+00:00","description":"Bash split string refers to splitting a string into multiple strings or elements based on various separator (delimiter).","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/18-Bash-Split-String.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/18-Bash-Split-String.png","width":400,"height":400,"caption":"Bash split string"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/#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":"8 Methods to Split String in Bash [With Examples]"}]},{"@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\/33986","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=33986"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/33986\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/34483"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=33986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=33986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=33986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}