{"id":36823,"date":"2024-02-14T08:20:14","date_gmt":"2024-02-14T08:20:14","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=36823"},"modified":"2024-03-31T06:19:10","modified_gmt":"2024-03-31T06:19:10","slug":"bash-string-functions","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/","title":{"rendered":"How to Use String Functions in Bash? [Examples Included]"},"content":{"rendered":"<p style=\"text-align: justify;\">You can follow these syntaxes to implement string functions in Bash:<\/p>\n<ol>\n<li>Declaring String with &#8220;declare&#8221; Command: <code class=\"\" data-line=\"\">declare a=&quot;string value&quot;<\/code><\/li>\n<li>Asking Input from User with &#8220;read&#8221; Command: <code class=\"\" data-line=\"\">read variable_name<\/code><\/li>\n<li>Determining Length of String: <code class=\"\" data-line=\"\">length=&quot;${#string_name}&quot;<\/code><\/li>\n<li>Extracting Substring from String: <code class=\"\" data-line=\"\">extracted_string=&quot;${original_string:start:end}&quot;<\/code><\/li>\n<li>Concatenating Strings in Bash: <code class=\"\" data-line=\"\">concatenated_string=${string1}${string2}<\/code><\/li>\n<li>Splitting Strings Into Arrays in Bash: <code class=\"\" data-line=\"\">read -a array_name &lt;&lt;&lt; &quot;$string_name&quot;<\/code><\/li>\n<\/ol>\n<p style=\"text-align: justify;\">Before using, a string variable must be declared properly, and then the string can be assigned to the declared string variable either manually or from user input. There are string functions in Bash for determining the length of a string, concatenating multiple strings into a single string, and splitting strings into arrays and individual words according to the specified <strong>Internal Field Separator(IFS)<\/strong>. In this article, I will briefly discuss these.<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/bash-string-functions-scripts.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 Work with String Functions in Bash<\/span><\/a><\/div>\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\/manipulation\/string-functions\/#1_Declaring_String_with_%E2%80%9Cdeclare%E2%80%9D_Command\" >1. Declaring String with &#8220;declare&#8221; Command<\/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\/manipulation\/string-functions\/#2_Asking_Input_from_User_with_%E2%80%9Cread%E2%80%9D_Command\" >2. Asking Input from User with &#8220;read&#8221; 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\/manipulation\/string-functions\/#3_Determining_Length_of_String\" >3. Determining Length of String<\/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\/manipulation\/string-functions\/#4_Extracting_Substring_from_String\" >4. Extracting Substring from String<\/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\/manipulation\/string-functions\/#5_Removing_Matched_Substring_from_Original_String\" >5. Removing Matched Substring from Original String<\/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\/manipulation\/string-functions\/#6_Concatenating_Strings_In_Bash\" >6. Concatenating Strings In Bash<\/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\/manipulation\/string-functions\/#7_Splitting_String_Based_on_Space\" >7. Splitting String Based on Space<\/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\/manipulation\/string-functions\/#8_Splitting_Strings_Into_Arrays_in_Bash\" >8. Splitting Strings Into Arrays in Bash<\/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\/manipulation\/string-functions\/#9_Returning_a_String_from_Bash_Functions_Using_Global_Variable\" >9. Returning a String from Bash Functions Using Global Variable<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/#10_Returning_a_String_from_Bash_Functions_Using_Local_Variable\" >10. Returning a String from Bash Functions Using Local Variable<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/#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-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/#Can_Bash_functions_return_strings\" >Can Bash functions return strings?<\/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\/manipulation\/string-functions\/#What_is_the_position_of_a_string_in_Bash\" >What is the position of 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-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/#How_to_connect_strings_in_Bash\" >How to connect strings in Bash?<\/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\/manipulation\/string-functions\/#How_to_split_a_string_based_on_string_Bash\" >How to split a string based on string Bash?<\/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\/manipulation\/string-functions\/#How_to_find_and_replace_string_Bash\" >How to find and replace string Bash?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"1_Declaring_String_with_%E2%80%9Cdeclare%E2%80%9D_Command\"><\/span>1. Declaring String with &#8220;declare&#8221; Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The <a href=\"https:\/\/linuxsimply.com\/declare-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>declare command<\/strong><\/a> declares a variable. The syntax for declaring a variable and assigning a value to that variable is: declare option variable_name=value. Here is a bash script for this:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\ndeclare -r a=&quot;Linuxsimply&quot;\necho &quot;$a&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>The <code class=\"\" data-line=\"\">declare -r a=&quot;Linuxsimply&quot;<\/code> declares a variable a. The <code class=\"\" data-line=\"\">-r<\/code> option makes the variable read-only, which means it can not be changed.<\/p>\n<\/div><\/div><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-36840 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/1-declare-1.png\" alt=\"A string variable is declared using string function in bash then printed.\" width=\"635\" height=\"204\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/1-declare-1.png 635w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/1-declare-1-300x96.png 300w\" sizes=\"(max-width: 635px) 100vw, 635px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"2_Asking_Input_from_User_with_%E2%80%9Cread%E2%80%9D_Command\"><\/span>2. Asking Input from User with &#8220;read&#8221; Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">A value can be assigned to a variable by taking input from the user with the read command. The read command reads input from the user using the syntax: <code class=\"\" data-line=\"\">read variable_name<\/code>. Here is a bash script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nread -p &#039;Enter your name: &#039; a\necho &quot;Hello $a&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>The <code class=\"\" data-line=\"\">read -p &#039;Enter your name: &#039; a<\/code> takes input from the user and keeps it to the variable a. The <code class=\"\" data-line=\"\">-p<\/code> option to display &#8216;Enter your name: &#8216; in the prompt.<\/p>\n<\/div><\/div><img decoding=\"async\" class=\"aligncenter wp-image-36841 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/2-read.png\" alt=\"Input taken from user.\" width=\"583\" height=\"229\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/2-read.png 583w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/2-read-300x118.png 300w\" sizes=\"(max-width: 583px) 100vw, 583px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"3_Determining_Length_of_String\"><\/span>3. Determining Length of String<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The string contains various types of characters. To determine a string&#8217;s length, put \u2018#\u2019 before the string variable name. The syntax is <code class=\"\" data-line=\"\">length=&quot;${#variable_name}&quot;<\/code>. It will return the length of the string contained in variable_name. Here is a bash script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\na=&quot;Linuxsimply&quot;\nl=&quot;${#a}&quot;\necho $l<\/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>The<code class=\"\" data-line=\"\"> l=&quot;${#a}&quot;<\/code> calculates the length stored in the variable a and keeps it to the l variable. And the <a href=\"https:\/\/linuxsimply.com\/echo-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>echo command<\/strong><\/a> prints the value of l.<\/p>\n<\/div><\/div><img decoding=\"async\" class=\"aligncenter wp-image-36842 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/3-length.png\" alt=\"Length of string is determined using string function in Bash.\" width=\"615\" height=\"182\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/3-length.png 615w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/3-length-300x89.png 300w\" sizes=\"(max-width: 615px) 100vw, 615px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"4_Extracting_Substring_from_String\"><\/span>4. Extracting Substring from String<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Parameter expansion can extract a specific substring from a string. Use the syntax <code class=\"\" data-line=\"\">substring=&quot;${string:start:end}&quot;<\/code> to extract a substring from the original string. Here is the bash script to do so:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\na=&quot;LinuxSimply&quot;\nb=\u201d${a::5}\u201d\necho $b<\/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>The <code class=\"\" data-line=\"\">b=&quot;${a::5}&quot;<\/code> extracts a substring from the string stored in the variable \u2018a\u2019. Here, the <code class=\"\" data-line=\"\">a::5<\/code> specifies extracting from the beginning to the 5th character.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-36843 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/4-extract.png\" alt=\"A substring is extracted from original string.\" width=\"635\" height=\"187\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/4-extract.png 635w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/4-extract-300x88.png 300w\" sizes=\"(max-width: 635px) 100vw, 635px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"5_Removing_Matched_Substring_from_Original_String\"><\/span>5. Removing Matched Substring from Original String<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Parameter expansion can remove substring from a string. To remove a dot and a substring from the end of a string, use the syntax: <code class=\"\" data-line=\"\">new_string=&quot;${original_string%.*}&quot;<\/code>. Here is a complete bash script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\ndeclare -r a=&quot;linux.simply.sh&quot;\necho &quot;original string: $a&quot;\nb=&quot;${a%.*}&quot;\necho &quot;manipulated string: $b&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>The <code class=\"\" data-line=\"\">b=&quot;${a%.*}&quot;<\/code> removes the shortest match of \u2018.*\u2019, which stands for any characters followed by a dot from the ending of the string a and keeps the remaining part of the string to the b variable.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-36844 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/5-remove.png\" alt=\"Part of the original string is removed.\" width=\"619\" height=\"251\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/5-remove.png 619w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/5-remove-300x122.png 300w\" sizes=\"(max-width: 619px) 100vw, 619px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"6_Concatenating_Strings_In_Bash\"><\/span>6. Concatenating Strings In Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In Bash, concatenation means listing strings one after another. To do so, follow the syntax: <code class=\"\" data-line=\"\">concatenated_string=${string_1} ${string_2}<\/code>. Here is the complete bash script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\na=Linux\nb=Simply\nvar=${a}${b}\necho $var<\/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>The <code class=\"\" data-line=\"\">var=${a}${b}<\/code> concatenates the value of a and b and keeps it to the third variable, var.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-36845 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/6-concate.png\" alt=\"Two strings are concatenated.\" width=\"637\" height=\"206\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/6-concate.png 637w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/6-concate-300x97.png 300w\" sizes=\"(max-width: 637px) 100vw, 637px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"7_Splitting_String_Based_on_Space\"><\/span>7. Splitting String Based on Space<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The string contains several words separated by space. Set space as the Internal Field Separator(IFS) to split a string based on space. Here is the complete bash script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\na=&quot;Welcome to Linuxsimply&quot;\nIFS=&#039; &#039;\nread -a b &lt;&lt;&lt; &quot;$a&quot;\necho &quot;There are ${#b[*]} words in the text.&quot;\nfor c in &quot;${b[@]}&quot;;\ndo\n  printf &quot;$c\\n&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>The <code class=\"\" data-line=\"\">IFS=&#039; &#039;<\/code> sets the Internal Field Separator(IFS) variable to a space character. It is used by the read command to separate the words inside the string. The <code class=\"\" data-line=\"\">read -a b &lt;&lt;&lt; &quot;$a&quot;<\/code> reads the content of variable \u2018a\u2019 and splits it into an array \u2018b\u2019 using the space character specified in IFS. Finally, <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 array b and prints them in a newline.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-36846 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/7-split.png\" alt=\"The string is splitted based on space.\" width=\"617\" height=\"278\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/7-split.png 617w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/7-split-300x135.png 300w\" sizes=\"(max-width: 617px) 100vw, 617px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"8_Splitting_Strings_Into_Arrays_in_Bash\"><\/span>8. Splitting Strings Into Arrays in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The string stores words separated by space. To split a string into an array in Bash, use the syntax: <code class=\"\" data-line=\"\">read -a array_name &lt;&lt;&lt; &quot;$string_name&quot;<\/code>. Here, it will read string, then split it into fields and store them in an array. Below is a complete bash script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\na=&#039;Linux Simply&#039;\nread -a b &lt;&lt;&lt; &quot;$a&quot;\nfor c in &quot;${b[@]}&quot;;\ndo\n  printf &quot;$c\\n&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><code class=\"\" data-line=\"\">read -a b &lt;&lt;&lt; &quot;$a&quot;<\/code> reads the content of variable a and splits each word using the default value of the Internal Field Separator(IFS), which is whitespace including space, tab, and -a tells the read command to store it into an array \u2018b\u2019. Then, it prints every element in a new line.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-36847 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/8-array.png\" alt=\"The string has been splitted into an array utilizing string function in Bash.\" width=\"594\" height=\"224\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/8-array.png 594w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/8-array-300x113.png 300w\" sizes=\"(max-width: 594px) 100vw, 594px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"9_Returning_a_String_from_Bash_Functions_Using_Global_Variable\"><\/span>9. Returning a String from Bash Functions Using Global Variable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">A global variable is a variable that exists inside and outside of a function. A bash function can print a string using a global variable. This output can be captured after calling the function; the global variable value will be according to the function definition. Here is a complete bash script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nfunction FF()\n{\n  returnval=&#039;Linux Simply&#039;\n}\nreturnval=&#039;SOFTEKO&#039;\necho &quot;Before: $returnval&quot;\nFF\necho &quot;After: $returnval&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>The <code class=\"\" data-line=\"\">function FF()<\/code> defines a function named FF. <code class=\"\" data-line=\"\">returnval=&#039;SOFTEKO&#039;<\/code> assigns a string to returnval variable. Inside the function, returnval is assigned with a string. Then, another string is assigned to returnval at the outside of the function. And <code class=\"\" data-line=\"\">echo &quot;Before: $returnval&quot;<\/code> prints the value of variable returnval before executing the FF function. After that, FF calls the function. Finally, <code class=\"\" data-line=\"\">echo &quot;After: $returnval&quot;<\/code> prints the value of variable returnval after calling the function FF. Here, returnval is the global variable.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-36848 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/9-global.png\" alt=\"String returned from function.\" width=\"624\" height=\"211\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/9-global.png 624w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/9-global-300x101.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"10_Returning_a_String_from_Bash_Functions_Using_Local_Variable\"><\/span>10. Returning a String from Bash Functions Using Local Variable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Inside a function, a local variable can be defined with a value, and the variable&#8217;s value can be captured using command substitution. Here is an example of returning a string from <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/\" target=\"_blank\" rel=\"noopener\"><strong>bash functions<\/strong><\/a> using a local variable:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nfunction FF2()\n{\n  local\u00a0 returnval=&#039;Hello from LinuxSimply&#039;\n  echo &quot;$returnval&quot;\n}\nval=$(FF2)\necho $val<\/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>Inside the FF2 variable, a string is assigned to the local variable returnval, and the value of returnval is printed. The <code class=\"\" data-line=\"\">val=$(FF2)<\/code> calls the function and keeps the return string from the function to the val variable.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-36849 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/10-function.png\" alt=\"String value returned from function.\" width=\"594\" height=\"199\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/10-function.png 594w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/10-function-300x101.png 300w\" sizes=\"(max-width: 594px) 100vw, 594px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In conclusion, string manipulation allows users to update and edit strings according to necessity. There are many types of string functions that are discussed in this article. It will guide the learner in implementing these functions to achieve the intended task.<\/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=\"Can_Bash_functions_return_strings\"><\/span>Can Bash functions return strings?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Bash functions can not return strings directly. At first, use the echo command to print a string and capture the output when the function is called.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_position_of_a_string_in_Bash\"><\/span>What is the position of a string in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The bash strings are zero-indexed. The position of the first character is \u20180\u2019, that of the second character is \u20181\u2019, and so on.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_connect_strings_in_Bash\"><\/span>How to connect strings in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To connect or concatenate multiple strings into a string, place them one after another. If <code class=\"\" data-line=\"\">a=&quot;Good &quot; b=&quot;Morning&quot;<\/code> then <code class=\"\" data-line=\"\">greeting=&quot;$a$b&quot;<\/code> concatenates the strings a and b and keeps them to the variable greeting.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_split_a_string_based_on_string_Bash\"><\/span>How to split a string based on string Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To split a string into an array in Bash, use the syntax: <code class=\"\" data-line=\"\">read -a array_name &lt;&lt;&lt; &quot;$string_name&quot;<\/code>. It will split the words inside the string, and the -a option will enable it to store the words inside the array_name array.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_find_and_replace_string_Bash\"><\/span>How to find and replace string Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To find and replace a string in bash, use the syntax: <code class=\"\" data-line=\"\">echo &#039;Original String&#039; | sed &#039;s\/old_word\/new_word&#039;<\/code>. Here, the original string will be piped to the sed command, where the \u2018s\u2019 option stands for substituting the old_word with the new_word.<br \/>\n<script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"Can Bash functions return strings?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Bash functions can not return strings directly. At first, use the echo command to print a string and capture the output when the function is called.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is the position of a string in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"The bash strings are zero-indexed. The position of the first character is \u20180\u2019, that of the second character is \u20181\u2019, and so on.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How to connect strings in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To connect or concatenate multiple strings into a string, place them one after another. If a=\\\"Good \\\" b=\\\"Morning,\\\" then greeting=\\\"$a$b\\\" concatenates the strings a and b and keeps them to the variable greeting.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How to split a string based on string Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To split a string into an array in Bash, use the syntax: read -a array_name <<< \\\"$string_name\\\". It will split the words inside the string, and the -a option will enable it to store the words inside the array_name array.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How to find and replace string Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To find and replace a string in bash, use the syntax: echo 'Original String' | sed 's\/old_word\/new_word'. Here, the original string will be piped to the sed command, where the \u2018s\u2019 option stands for substituting the old_word with the new_word.\"\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\/manipulation\/string-to-int\/\" target=\"_blank\" rel=\"noopener\">How to Convert a Bash String to Int? [8 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/format-string\/\" target=\"_blank\" rel=\"noopener\">How to Format a String in Bash? [Methods, Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/convert-to-uppercase\/\" target=\"_blank\" rel=\"noopener\">How to Convert Bash String to Uppercase? [7 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/bash-to-lowercase\/\" target=\"_blank\" rel=\"noopener\">How to Convert Bash String to Lowercase? [7 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-replace\/\" target=\"_blank\" rel=\"noopener\">How to Replace String in Bash? [5 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/trim-string\/\" target=\"_blank\" rel=\"noopener\">How to Trim String in Bash? [6 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/truncate-string\/\" target=\"_blank\" rel=\"noopener\">How to Truncate String in Bash? [5 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/remove-character\/\" target=\"_blank\" rel=\"noopener\">How to Remove Character from String in Bash? [7+ Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/remove-first-character\/\" target=\"_blank\" rel=\"noopener\">How to Remove First Character From Bash String? [7 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/remove-last-character\/\" target=\"_blank\" rel=\"noopener\">How to Remove Last Character from Bash String [6 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/generate-random-string\/\" target=\"_blank\" rel=\"noopener\">How to Generate a Random String in Bash? [8 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/\" target=\"_blank\" rel=\"noopener\">How to Manipulate Bash String Array [5 Ways]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/here-string\/\" target=\"_blank\" rel=\"noopener\">How to Use \u201cHere String\u201d in Bash? [Basic to Advance]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/base64-encode-decode\/\" target=\"_blank\" rel=\"noopener\">Encode and Decode with \u201cbase64\u201d in Bash [6 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/bash-eval\/\" target=\"_blank\" rel=\"noopener\">How to Use \u201ceval\u201d Command in Bash? [8 Practical Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/\" target=\"_blank\" rel=\"noopener\">How to Get the First Character from Bash String? [8 Methods]<\/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;String Manipulation in Bash&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\/manipulation\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/\" target=\"_blank\" rel=\"noopener\">String Manipulation in Bash<\/a><\/span> | <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;36823&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;How to Use String Functions in Bash? [Examples Included]&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            <span class=\"kksr-muted\">Rate this post<\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>You can follow these syntaxes to implement string functions in Bash: Declaring String with &#8220;declare&#8221; Command: declare a=&quot;string value&quot; Asking &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How to Use String Functions in Bash? [Examples Included]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/#more-36823\" aria-label=\"Read more about How to Use String Functions in Bash? [Examples Included]\">Read more<\/a><\/p>\n","protected":false},"author":314894,"featured_media":36850,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[192],"class_list":["post-36823","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-string-manipulation","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Use String Functions in Bash? [Examples Included] - LinuxSimply<\/title>\n<meta name=\"description\" content=\"Bash string functions enables users to do necessary string manipulation work to keep the system updated and efficient.\" \/>\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\/manipulation\/string-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use String Functions in Bash? [Examples Included] - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"Bash string functions enables users to do necessary string manipulation work to keep the system updated and efficient.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/dasgupta.susmit\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-14T08:20:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-31T06:19:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/bash-string-functions-feature-image.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Susmit Das Gupta\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Susmit Das Gupta\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 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\\\/manipulation\\\/string-functions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-functions\\\/\"},\"author\":{\"name\":\"Susmit Das Gupta\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/38abbef9a1053d9faec8935dc39f10fa\"},\"headline\":\"How to Use String Functions in Bash? [Examples Included]\",\"datePublished\":\"2024-02-14T08:20:14+00:00\",\"dateModified\":\"2024-03-31T06:19:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-functions\\\/\"},\"wordCount\":1446,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-functions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/bash-string-functions-feature-image.png\",\"keywords\":[\"string manipulation\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-functions\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-functions\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-functions\\\/\",\"name\":\"How to Use String Functions in Bash? [Examples Included] - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-functions\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-functions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/bash-string-functions-feature-image.png\",\"datePublished\":\"2024-02-14T08:20:14+00:00\",\"dateModified\":\"2024-03-31T06:19:10+00:00\",\"description\":\"Bash string functions enables users to do necessary string manipulation work to keep the system updated and efficient.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-functions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-functions\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-functions\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/bash-string-functions-feature-image.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/bash-string-functions-feature-image.png\",\"width\":400,\"height\":400,\"caption\":\"Bash string functions.\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-functions\\\/#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\":\"String Manipulation in Bash\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"How to Use String Functions in Bash? [Examples Included]\"}]},{\"@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\\\/38abbef9a1053d9faec8935dc39f10fa\",\"name\":\"Susmit Das Gupta\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Susmit-Das-Gupta-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Susmit-Das-Gupta-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Susmit-Das-Gupta-II-96x96.png\",\"caption\":\"Susmit Das Gupta\"},\"description\":\"Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/dasgupta.susmit\",\"https:\\\/\\\/www.instagram.com\\\/susmit_dasgupta\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/susmitdasgupta\\\/\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/susmit\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Use String Functions in Bash? [Examples Included] - LinuxSimply","description":"Bash string functions enables users to do necessary string manipulation work to keep the system updated and efficient.","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\/manipulation\/string-functions\/","og_locale":"en_US","og_type":"article","og_title":"How to Use String Functions in Bash? [Examples Included] - LinuxSimply","og_description":"Bash string functions enables users to do necessary string manipulation work to keep the system updated and efficient.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/dasgupta.susmit","article_published_time":"2024-02-14T08:20:14+00:00","article_modified_time":"2024-03-31T06:19:10+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/bash-string-functions-feature-image.png","type":"image\/png"}],"author":"Susmit Das Gupta","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Susmit Das Gupta","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/"},"author":{"name":"Susmit Das Gupta","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/38abbef9a1053d9faec8935dc39f10fa"},"headline":"How to Use String Functions in Bash? [Examples Included]","datePublished":"2024-02-14T08:20:14+00:00","dateModified":"2024-03-31T06:19:10+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/"},"wordCount":1446,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/bash-string-functions-feature-image.png","keywords":["string manipulation"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/","name":"How to Use String Functions in Bash? [Examples Included] - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/bash-string-functions-feature-image.png","datePublished":"2024-02-14T08:20:14+00:00","dateModified":"2024-03-31T06:19:10+00:00","description":"Bash string functions enables users to do necessary string manipulation work to keep the system updated and efficient.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/bash-string-functions-feature-image.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/bash-string-functions-feature-image.png","width":400,"height":400,"caption":"Bash string functions."},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/#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":"String Manipulation in Bash","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/"},{"@type":"ListItem","position":5,"name":"How to Use String Functions in Bash? [Examples Included]"}]},{"@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\/38abbef9a1053d9faec8935dc39f10fa","name":"Susmit Das Gupta","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Susmit-Das-Gupta-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Susmit-Das-Gupta-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Susmit-Das-Gupta-II-96x96.png","caption":"Susmit Das Gupta"},"description":"Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio","sameAs":["https:\/\/www.facebook.com\/dasgupta.susmit","https:\/\/www.instagram.com\/susmit_dasgupta\/","https:\/\/www.linkedin.com\/in\/susmitdasgupta\/"],"url":"https:\/\/linuxsimply.com\/author\/susmit\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/36823","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\/314894"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=36823"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/36823\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/36850"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=36823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=36823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=36823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}