{"id":38628,"date":"2024-03-14T08:21:58","date_gmt":"2024-03-14T08:21:58","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=38628"},"modified":"2024-03-31T06:20:33","modified_gmt":"2024-03-31T06:20:33","slug":"bash-get-first-character-of-string","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/","title":{"rendered":"How to Get the First Character from Bash String? [8 Methods]"},"content":{"rendered":"<p style=\"text-align: justify;\">In Bash scripting, extracting or getting the character is important for formatting and analyzing strings such as to validate, parse, and process texts.<\/p>\n<p style=\"text-align: justify;\">To get the first character of a string in Bash, check the following methods:<\/p>\n<ol>\n<li>Using parameter expansion: <code class=\"\" data-line=\"\">${parameter:start_index:length}<\/code><\/li>\n<li>Using the cut command: <code class=\"\" data-line=\"\">echo &quot;$input_string&quot; | cut -cN<\/code><\/li>\n<li>Using the sed command: <code class=\"\" data-line=\"\">sed\u00a0 &#039;s\/^\\(.\\).*\/\\1\/&#039;<\/code><\/li>\n<li>Using the grep command: <code class=\"\" data-line=\"\">grep -o &#039;action&#039;<\/code><\/li>\n<li>Using the awk command: <code class=\"\" data-line=\"\">echo &quot;input_str&quot; | awk &#039;{print substr($input_str,start_index, length)}&#039;<br \/>\n<\/code><\/li>\n<li>Using the printf command: <code class=\"\" data-line=\"\">printf &quot;%.1s&quot; &quot;$input_string&quot;<\/code><\/li>\n<li>Using the expr command: <code class=\"\" data-line=\"\">expr substr &quot;$input_string&quot; start_index length<\/code><\/li>\n<li>Using the perl command:\u00a0 <code class=\"\" data-line=\"\">echo &quot;$input_string&quot; | perl [option] &#039;action&#039;<\/code><\/li>\n<\/ol>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/Practice_Files-Get_First_Character_of_String_in_Bash.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 Get the First Character From String in Bash<\/span><\/a><\/div>\n<p style=\"text-align: justify;\">Dive into the article to learn these methods of how to use Bash to get the first character from a string in detail.<\/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\/manipulation\/get-first-character\/#1_Using_Parameter_Expansion\" >1. Using Parameter Expansion<\/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\/get-first-character\/#2_Using_%E2%80%9Ccut%E2%80%9D_Command\" >2. 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-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/#3_Using_%E2%80%9Csed%E2%80%9D_Command\" >3. 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-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/#4_Using_%E2%80%9Cgrep%E2%80%9D_Command\" >4. Using \u201cgrep\u201d Command<\/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\/get-first-character\/#5_Using_%E2%80%9Cawk%E2%80%9D_Command\" >5. Using \u201cawk\u201d Command<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/#How_to_Get_the_First_Character_from_Multiple_Line_Input\" >How to Get the First Character from Multiple Line Input?<\/a><\/li><\/ul><\/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\/get-first-character\/#6_Using_%E2%80%9Cprintf%E2%80%9D_Command\" >6. Using \u201cprintf\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\/manipulation\/get-first-character\/#7_Using_%E2%80%9Cexpr%E2%80%9D_Command\" >7. Using \u201cexpr\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\/manipulation\/get-first-character\/#8_Using_%E2%80%9Cperl%E2%80%9D_Command\" >8. Using \u201cperl\u201d Command<\/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\/get-first-character\/#How_to_Get_the_First_Character_of_Each_Element_of_an_Array\" >How to Get the First Character of Each Element of an Array?<\/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\/get-first-character\/#How_to_Get_the_First_Character_of_Each_Substring\" >How to Get the First Character of Each Substring?<\/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\/get-first-character\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/#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-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/#Can_I_get_the_first_character_from_an_input_string\" >Can I get the first character from an input string?<\/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\/get-first-character\/#How_do_I_get_the_first_word_from_a_string\" >How do I get the first word from a 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\/manipulation\/get-first-character\/#How_to_get_the_second_character_from_a_string\" >How to get the second character from a string?<\/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\/get-first-character\/#What_is_the_first_position_of_a_string_in_Bash\" >What is the first 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-18\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/#How_to_check_if_the_first_character_of_a_string_is_a_slash\" >How to check if the first character of a string is a slash?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"1_Using_Parameter_Expansion\"><\/span>1. 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> allows you to retrieve and manipulate the value stored in the parameter or variable. With parameter expansion, you can easily get the first character of a string. The basic syntax for extracting a substring or character from an input string:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">${parameter:offset:length}<\/code><\/pre>\n<p style=\"text-align: justify;\"><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;\">Replace the <strong>parameter<\/strong> with the input string, <strong>offset<\/strong> with the position you want to extract the character, and <strong>length<\/strong> with how many characters you want to extract. <\/div><\/div>\n<p style=\"text-align: justify;\">To get the first character from a bash string using parameter expansion, check the following script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define the string\ninput_string=&quot;This is the first string.&quot;\n\n# Extract the first character\nfirst_char=&quot;${input_string:0:1}&quot;\n\n# Print the first character\necho &quot;The extracted first character: $first_char&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;\">Firstly the script declares the input string in a variable named <code class=\"\" data-line=\"\">input_string<\/code>. Then <code class=\"\" data-line=\"\">${input_string:0:1}<\/code> extracts the input string&#8217;s first character and stores it in a variable named <code class=\"\" data-line=\"\">first_char<\/code>. At last, the <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/input-output\/output\/echo-command\/\" target=\"_blank\" rel=\"noopener\"><strong>echo command <\/strong><\/a>prints the output in the terminal.<\/div><\/div>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-38635 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/1.-parameter.png\" alt=\"Get the first character using parameter expansion\" width=\"670\" height=\"209\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/1.-parameter.png 670w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/1.-parameter-300x94.png 300w\" sizes=\"(max-width: 670px) 100vw, 670px\" \/><\/p>\n<p style=\"text-align: justify;\">The output shows that the script has successfully gotten the first character \u201cT\u201d from the input string &#8220;This is the first string.&#8221;.<\/p>\n<p style=\"text-align: justify;\"><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> To store the result in a file, use the following code:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo &quot;The extracted first character: $first_char&quot; &gt; &quot;$output_file&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\">This command stores the character in the file name <strong>output_file<\/strong>. If the file does not exist then this command will create a new file otherwise it <strong>overwrites<\/strong> the existing content. <\/div><\/div>\n<h2><span class=\"ez-toc-section\" id=\"2_Using_%E2%80%9Ccut%E2%80%9D_Command\"><\/span>2. Using \u201ccut\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In bash scripting, the <a href=\"https:\/\/linuxsimply.com\/cut-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>cut command<\/strong><\/a> mainly extracts characters, bytes, and fields from the input string. Use the <code class=\"\" data-line=\"\">-c<\/code> option with following a number which indicates the position of the character you want to extract. For example, if you want to get the <strong>Nth<\/strong> character from an input string, you can use <strong>-cN<\/strong> which will extract the specific character.<\/p>\n<p style=\"text-align: justify;\">To get the first character from a string using the <code class=\"\" data-line=\"\">cut<\/code> command, check out the script below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define the string\ninput_string=&quot;Another string.&quot;\n\n# Get the first character using cut command\nfirst_char=$(echo &quot;$input_string&quot; | cut -c1)\n\n# Print the first character\necho &quot;The first character of the input string: $first_char&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">The <code class=\"\" data-line=\"\">cut<\/code> command takes the output of the <code class=\"\" data-line=\"\">echo<\/code> command through the <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/piping\/\" target=\"_blank\" rel=\"noopener\"><strong>pipe operator (|)<\/strong><\/a>. The <code class=\"\" data-line=\"\">cut -c1<\/code> cuts the input string&#8217;s first character and stores it in a new variable named <code class=\"\" data-line=\"\">first_char<\/code>.<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-38637 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/2.-cut.png\" alt=\"Using cut command to get the first character \" width=\"700\" height=\"239\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/2.-cut.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/2.-cut-300x102.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>The script has gotten the first character \u201cA\u201d from the input string &#8220;Another string.&#8221;.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"3_Using_%E2%80%9Csed%E2%80%9D_Command\"><\/span>3. Using \u201csed\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The <a href=\"https:\/\/linuxsimply.com\/sed-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>sed command<\/strong><\/a> is a simple yet very useful command to remove characters, replace strings, match patterns, and manipulate files and texts. The basic syntax to get the first character from a string is <code class=\"\" data-line=\"\">s\/^\\(.\\).*\/\\1\/<\/code>. To get the first character from the input string using the <code class=\"\" data-line=\"\">sed<\/code> command check out the following script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define the string\ninput_string=&quot;Hello, world!&quot;\n\n# Get the first character using sed\nfirst_char=$(echo &quot;$input_string&quot; | sed &#039;s\/^\\(.\\).*\/\\1\/&#039;)\n\n# Print the first character\necho &quot;The first character: $first_char&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;\">At first, the script declares the input string in a variable named <code class=\"\" data-line=\"\">input_string<\/code>. After that, the <strong>pipe operator (|)<\/strong> redirects the output of the <code class=\"\" data-line=\"\">echo<\/code> command to the <code class=\"\" data-line=\"\">sed<\/code> command. In the <strong>sed command<\/strong>, the<code class=\"\" data-line=\"\">^<\/code>matched the starting of the input string. Then the<code class=\"\" data-line=\"\">\\(.\\)<\/code>catches the first character of the string. After doing this, the sed command matches the rest of the characters using the<code class=\"\" data-line=\"\">.*<\/code>. At last, the <code class=\"\" data-line=\"\">\\1<\/code>replaces all the remaining characters with the first character.<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-38638 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/3.-sed.png\" alt=\"Getting the first character using sed command\" width=\"575\" height=\"208\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/3.-sed.png 575w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/3.-sed-300x109.png 300w\" sizes=\"(max-width: 575px) 100vw, 575px\" \/><\/p>\n<p style=\"text-align: justify;\">The script has extracted the first character \u201cH\u201d from the input string \u2018Hello, world!\u2019.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"4_Using_%E2%80%9Cgrep%E2%80%9D_Command\"><\/span>4. Using \u201cgrep\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The <a href=\"https:\/\/linuxsimply.com\/grep-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>grep command<\/strong><\/a> is an easy-to-search and match pattern of characters of a string and file. Using the grep command with the <code class=\"\" data-line=\"\">-o<\/code> option will print the match character or line from the input string.<\/p>\n<p style=\"text-align: justify;\">To get the first character from an input string using the <code class=\"\" data-line=\"\">grep<\/code> command, use the following script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define the string\ninput_string=&quot;Hello, world!&quot;\n\n# Get the first character using grep\nfirst_char=$(echo &quot;$input_string&quot; | grep -o &#039;^.&#039;)\n\n# Print the first character\necho &quot;The first character: $first_char&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">The <strong>pipe operator<\/strong> redirects the output of the <strong>echo command<\/strong> to the<code class=\"\" data-line=\"\">grep<\/code>command. Then, the <code class=\"\" data-line=\"\">grep -o &#039;^.&#039;<\/code>matches the first character of the input string. Then the<code class=\"\" data-line=\"\">echo<\/code>command prints only the matched character.\u00a0 <\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-38641 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/4.-grep.png\" alt=\"Bash get the first character of string using grep command\" width=\"582\" height=\"213\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/4.-grep.png 582w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/4.-grep-300x110.png 300w\" sizes=\"(max-width: 582px) 100vw, 582px\" \/><\/p>\n<p style=\"text-align: justify;\">The output shows that the script has extracted the first character \u201cH\u201d from the input string \u2018Hello, world!\u2019.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"5_Using_%E2%80%9Cawk%E2%80%9D_Command\"><\/span>5. Using \u201cawk\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">To get the first character from the input string, you can use the <strong>awk command<\/strong> with <code class=\"\" data-line=\"\">substr()<\/code> function. The<code class=\"\" data-line=\"\">awk<\/code>is a versatile command tool used for text processing, handling multi-byte characters, and especially for pattern matching and manipulation. Check the following script below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define the string\ninput_string=&quot;# a special character in bash&quot;\n\n# Get the first character using awk\nfirst_char=$(echo &quot;$input_string&quot; | awk &#039;{print substr($0, 1, 1)}&#039;)\n\n# Print the first character\necho &quot;The first character: $first_char&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;\">At first, the script declares the input string named<code class=\"\" data-line=\"\">input_string<\/code>. The <strong>pipe operator<\/strong> takes the output of the<code class=\"\" data-line=\"\">echo<\/code>command to the<code class=\"\" data-line=\"\">awk<\/code>command. In the <a href=\"https:\/\/linuxsimply.com\/awk-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>awk command<\/strong><\/a>, the substr function extracts a substring. The<code class=\"\" data-line=\"\">$0<\/code>refers to the entire input string. Then the first<code class=\"\" data-line=\"\">1<\/code>indicates the starting position of the substring and the second<code class=\"\" data-line=\"\">1<\/code>indicates the length of the substring which will extract the first character as both the parameters are set at <code class=\"\" data-line=\"\">1<\/code>. <\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-38644 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/5.-awk.png\" alt=\"Get the first character of string in bash using awk command\" width=\"660\" height=\"213\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/5.-awk.png 660w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/5.-awk-300x97.png 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/p>\n<p style=\"text-align: justify;\">From the image, you can see the script has successfully extracted the first character \u201c#\u201d from the input string.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_Get_the_First_Character_from_Multiple_Line_Input\"><\/span>How to Get the First Character from Multiple Line Input?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">When there are multiple newlines in an input string, then you can use the <strong>awk command<\/strong> to get the first character. Check out the following script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define the input string with multiple lines\ninput_string=$&#039;The First line. \\nIt is the Second line. \\nHere is the Third line.&#039;\n\n# Use awk to extract the first character from each line\nfirst_char=$(echo &quot;$input_string&quot; | awk &#039;{print substr($0, 1, 1)}&#039;)\n\n# Print the first characters\necho &quot;First characters:&quot;\necho &quot;$first_char&quot;<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-38645 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/6.-awk-again.png\" alt=\"Get the first character of multiple line \" width=\"685\" height=\"261\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/6.-awk-again.png 685w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/6.-awk-again-300x114.png 300w\" sizes=\"(max-width: 685px) 100vw, 685px\" \/><\/p>\n<p>The image shows that the script has got the first characters \u201cT\u201d, \u201cI\u201d and \u201cH\u201d from each line of the input strings.<\/p>\n<p style=\"text-align: justify;\"><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>While declaring the string, use the <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/quotes\/single-quotes\/\" target=\"_blank\" rel=\"noopener\"><strong>single quote<\/strong><\/a>. Otherwise, it won\u2019t show the expected result.<\/div><\/div>\n<h2><span class=\"ez-toc-section\" id=\"6_Using_%E2%80%9Cprintf%E2%80%9D_Command\"><\/span>6. Using \u201cprintf\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The <strong>printf command<\/strong> in Linux is used to print and format the string, number, and special characters in the terminal. If you want to get the first character from a string using the <code class=\"\" data-line=\"\">printf<\/code> command, then you have to use the format specifier that specifies the first character that will print in the terminal.<\/p>\n<p style=\"text-align: justify;\">Navigate through the script below to get the first character from a string using the <code class=\"\" data-line=\"\">printf<\/code> command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define the string\ninput_string=&quot;Hello, world!&quot;\n\n# Get the first character using printf\ncharacter=$(printf &quot;%.1s&quot; &quot;$input_string&quot;)\n\n# Print the first character\necho &quot;The first character of the input string: $character&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;\">In the<code class=\"\" data-line=\"\">printf<\/code>command,<code class=\"\" data-line=\"\">%.1s<\/code>format specifier prints the first character of the input string and the script stores the value to a variable named<code class=\"\" data-line=\"\">character<\/code>. Then<code class=\"\" data-line=\"\">echo<\/code>command prints the final output.<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-38648 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/7.-printf.png\" alt=\"Bash get the first character of string using printf command\" width=\"700\" height=\"204\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/7.-printf.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/7.-printf-300x87.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p style=\"text-align: justify;\">The output shows that the printf command prints only the first character \u201cH\u201d of the input string.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"7_Using_%E2%80%9Cexpr%E2%80%9D_Command\"><\/span>7. Using \u201cexpr\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Using the<code class=\"\" data-line=\"\">substr<\/code>function, you can utilize the expr command to get the first character from the input string. The basic syntax for getting any character or substring is:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">expr substr &quot;$input_string&quot; [position] [length]<\/code><\/pre>\n<p style=\"text-align: justify;\">To get the first character from the input string using the<code class=\"\" data-line=\"\">expr<\/code>command, take a look at the following script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define the string\ninput_string=&quot;Last input string.&quot;\n\n# Get the first character using expr\nfirst_char=$(expr substr &quot;$input_string&quot; 1 1)\n\n# Print the first character\necho &quot;The first character: $first_char&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">The<code class=\"\" data-line=\"\">substr<\/code>function extracts the character at position<code class=\"\" data-line=\"\">1<\/code>taking the length of<code class=\"\" data-line=\"\">1<\/code>character and the script stores the output to a variable named<code class=\"\" data-line=\"\">first_char<\/code>. At last, the <strong>echo command<\/strong> prints the value of the string.<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-38649 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/8.-expr.png\" alt=\"Using the expr command get the first character of string in bash\" width=\"608\" height=\"207\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/8.-expr.png 608w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/8.-expr-300x102.png 300w\" sizes=\"(max-width: 608px) 100vw, 608px\" \/><\/p>\n<p>The script has extracted the first character \u201cL\u201d from the input string.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"8_Using_%E2%80%9Cperl%E2%80%9D_Command\"><\/span>8. Using \u201cperl\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In bash scripting, the <strong>perl command<\/strong> gets the first character from the input string by using the regular expression and slicing. In the slicing process, the script defines the starting position and length of the desired substring.<\/p>\n<p style=\"text-align: justify;\">To get the first character from the input string using<code class=\"\" data-line=\"\">perl<\/code>command, check the following script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define the input string\ninput_string=&quot;Hello, world!&quot;\n\n# Use Perl to extract the first character\nfirst_char=$(perl -e &quot;print substr(&#039;$input_string&#039;, 0, 1)&quot;)\n\n# Print the result\necho &quot;The first character: $first_char&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;\">In the <code class=\"\" data-line=\"\">$(perl -e &quot;print substr(&#039;$input_string&#039;, 0, 1)&quot;)<\/code>, the <code class=\"\" data-line=\"\">-e<\/code> option executes the <strong>perl code<\/strong> inline. Then it uses the substr function to extract the character. Here, the<code class=\"\" data-line=\"\">0<\/code>indicates the starting position of the character and<code class=\"\" data-line=\"\">1<\/code>indicates the length of the character to be extracted.<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-38651 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/9.-perl.png\" alt=\"bash get the first character of string using the perl command\" width=\"615\" height=\"209\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/9.-perl.png 615w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/9.-perl-300x102.png 300w\" sizes=\"(max-width: 615px) 100vw, 615px\" \/><\/p>\n<p style=\"text-align: justify;\">The image shows that the <strong>perl command<\/strong> has extracted the first character \u201cH\u201d from the input string.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_Get_the_First_Character_of_Each_Element_of_an_Array\"><\/span>How to Get the First Character of Each Element of an Array?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">To get the first character of each element of an array, you can use <strong>parameter expansion<\/strong> inside a <strong>loop<\/strong> where the loop iterates over each element and extracts the first character from each element string of the input array.<\/p>\n<p style=\"text-align: justify;\">The following script below shows how to do it:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define an array\ninput_array=(&quot;apple&quot; &quot;banana&quot; &quot;cherry&quot; &quot;date&quot;)\n\n# Loop through the array and extract the first character of each element\nfor element in &quot;${input_array[@]}&quot;; do\n   first_char=&quot;${element:0:1}&quot;\n   echo &quot;$first_char&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;\">Firstly the script declares an array with four elements in a variable named<code class=\"\" data-line=\"\">input_array<\/code>. Then, it uses a <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/\" target=\"_blank\" rel=\"noopener\"><strong>for loop<\/strong><\/a> that iterates over each element of the input array. The<code class=\"\" data-line=\"\">${input_array[@]}<\/code>expands each element for the <strong>for loop<\/strong>. Inside the loop, the script uses the parameter expansion <code class=\"\" data-line=\"\">${element:0:1}<\/code>that extracts a character starting from the index position<code class=\"\" data-line=\"\">0<\/code>, and the length of the substring is defined using<code class=\"\" data-line=\"\">1<\/code>. After that, th <code class=\"\" data-line=\"\">echo<\/code>command prints each of the values.<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-38653 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/10.-array.png\" alt=\"Extracting the first character of each element of an array\" width=\"679\" height=\"240\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/10.-array.png 679w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/10.-array-300x106.png 300w\" sizes=\"(max-width: 679px) 100vw, 679px\" \/><\/p>\n<p style=\"text-align: justify;\">The image shows that the script has got the first characters <strong>a,b,c,<\/strong> and <strong>d<\/strong> from the elements \u201capple\u201d, \u201cbanana\u201d, \u201ccherry\u201d and \u201cdate\u201d of the input array respectively.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_Get_the_First_Character_of_Each_Substring\"><\/span>How to Get the First Character of Each Substring?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Generally, a large string contains multiple substrings. To get each first character from the input string, first split the string into substrings using space (<code class=\"\" data-line=\"\">&quot; &quot;<\/code>) as the delimiter and then loop over the substrings to get the first character. Take a look at the following script for example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define the input string\nstring=&quot;This is a string.&quot;\n\n# Define the delimiter\ndelimiter=&quot; &quot;\n\n# Use the delimiter to split the input string into substrings\nIFS=&quot;$delimiter&quot; read -ra substrings &lt;&lt;&lt; &quot;$string&quot;\n\n# Iterate over each substring and extract the first character\nfor substring in &quot;${substrings[@]}&quot;; do\n   first_char=&quot;${substring:0:1}&quot;\n   echo &quot;$first_char&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;\">Firstly the script splits the input string into multiple substrings based on the <strong>delimiter<\/strong> which is set as <strong>space<\/strong>. Then with <code class=\"\" data-line=\"\">read -ra substrings &lt;&lt;&lt; &quot;$string&quot;<\/code>, the<code class=\"\" data-line=\"\">read<\/code>command reads the substrings, and the<code class=\"\" data-line=\"\">-ra<\/code>option tells the read command to store the substrings into an array named <code class=\"\" data-line=\"\">substrings<\/code>. After that, the <strong>for loop<\/strong> iterates over each element of the array. By using <code class=\"\" data-line=\"\">${substring:0:1}<\/code>, the loop extracts each of the first characters. At last, the<code class=\"\" data-line=\"\">echo<\/code>command inside the loop, prints the first character of the input string.<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-38655 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/11.-multichar-2.png\" alt=\"Extracting multichar from multiple lines of an input string\" width=\"675\" height=\"246\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/11.-multichar-2.png 675w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/11.-multichar-2-300x109.png 300w\" sizes=\"(max-width: 675px) 100vw, 675px\" \/><\/p>\n<p style=\"text-align: justify;\">The output shows that the script has extracted each of the characters from the substrings of the input string.<\/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, this article illustrates several methods to get the first characters from an input string. It also demonstrates how you can get the first character from multiple lines, from each element of the array. Among these methods, using the sed, perl, and awk commands offers more flexibility and versatility than others.<\/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_I_get_the_first_character_from_an_input_string\"><\/span>Can I get the first character from an input string?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Yes<\/strong>, you can get the first character simply using the parameter expansion. In the parameter expansion use the variable that contains the input string. To extract the character you need to specify the parameter for the substring. For example to extract the first character you have to use the <code class=\"\" data-line=\"\">${input_string:0:1}<\/code>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_get_the_first_word_from_a_string\"><\/span>How do I get the first word from a string?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To get the first word, you can use any method such as parameter expansion, <strong>awk<\/strong>, <strong>cut,<\/strong> <strong>perl<\/strong> command. For example, if you want to extract the first word from a string \u201cHello World!\u201d, then use <code class=\"\" data-line=\"\">$(echo &quot;$string&quot; | awk &#039;{print $1}&#039;)<\/code>. In this code, the awk command takes the output of the <code class=\"\" data-line=\"\">echo<\/code> command through the pipe operator. In the a<strong>wk command<\/strong>, the <code class=\"\" data-line=\"\">{print $1}<\/code> prints the first word of the input string.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_get_the_second_character_from_a_string\"><\/span>How to get the second character from a string?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To get the second character from a string, you can use parameter expansion and other external commands such as <code class=\"\" data-line=\"\">cut<\/code>, <code class=\"\" data-line=\"\">awk<\/code>, <code class=\"\" data-line=\"\">sed<\/code>, and <code class=\"\" data-line=\"\">per<\/code>l. For example, the command <code class=\"\" data-line=\"\">$(echo &quot;$string&quot; | cut -c2<\/code>) extracts the second character from the variable $string.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_first_position_of_a_string_in_Bash\"><\/span>What is the first position of a string in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The first position of a string starts with index <strong>0<\/strong>. Some programming languages use the first index as <strong>1<\/strong>. In Bash, the first index is represented with <code class=\"\" data-line=\"\">0<\/code>. For example, you want to show the first character of a string, then use the string variable with 0 indexing like <code class=\"\" data-line=\"\">first_char=&quot;${string:0:1}&quot;<\/code><\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_check_if_the_first_character_of_a_string_is_a_slash\"><\/span>How to check if the first character of a string is a slash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To check whether the first character of a string is a slash or not, then you can use the conditional expression if <code class=\"\" data-line=\"\">[ &quot;${string:0:1}&quot; = &quot;\/&quot; ]<\/code>. The expression compares whether the first character is slash or not.<\/p>\n<p style=\"text-align: justify;\">Check out the following script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&quot;\/ hello&quot;\n\nif [ &quot;${string:0:1}&quot; = &quot;\/&quot; ]; then\n   echo &quot;The first character is a slash.&quot;\nelse\n   echo &quot;The first character is not a slash.&quot;\nfi\n\n#Output: The first character is a slash.<\/code><\/pre>\n<p style=\"text-align: justify;\">If the condition is <strong>true<\/strong> (first character is \/) then it prints \u201cThe first character is a slash\u201d.<\/p>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"Can I get the first character from an input string?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Yes, you can get the first character simply using the parameter expansion. In the parameter expansion use the variable that contains the input string. To extract the character you need to specify the parameter for the substring. For example to extract the first character you have to use the ${input_string:0:1}.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How do I get the first word from a string?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To get the first word, you can use any method such as parameter expansion, awk, cut, perl command. For example, if you want to extract the first word from a string \u201cHello World!\u201d, then use $(echo \\\"$string\\\" | awk '{print $1}'). In this code, the awk command takes the output of the echo command through the pipe operator. In the awk command, the {print $1} prints the first word of the input string.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How to get the second character from a string?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To get the second character from a string, you can use parameter expansion and other external commands such as cut, awk, sed, and perl. For example, the command $(echo \\\"$string\\\" | cut -c2) extracts the second character from the variable $string.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is the first position of a string in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"The first position of a string starts with index 0. Some programming languages use the first index as 1. In Bash, the first index is represented with 0. For example, you want to show the first character of a string, then use the string variable with 0 indexing like first_char=\\\"${string:0:1}\\\"\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How to check if the first character of a string is a slash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To check whether the first character of a string is a slash or not, then you can use the conditional expression if [ \\\"${string:0:1}\\\" = \\\"\/\\\" ]. The expression compares whether the first character is slash or not.\"\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\/string-functions\/\" target=\"_blank\" rel=\"noopener\">How to Use String Functions in Bash? [Examples Included]<\/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<\/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;38628&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;4&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;4.7&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;4.7\\\/5 - (4 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;How to Get the First Character from Bash String? [8 Methods]&quot;,&quot;width&quot;:&quot;133.8&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: 133.8px;\">\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            4.7\/5 - (4 votes)    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>In Bash scripting, extracting or getting the character is important for formatting and analyzing strings such as to validate, parse, &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How to Get the First Character from Bash String? [8 Methods]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/#more-38628\" aria-label=\"Read more about How to Get the First Character from Bash String? [8 Methods]\">Read more<\/a><\/p>\n","protected":false},"author":314907,"featured_media":38631,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[192],"class_list":["post-38628","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 Get the First Character from Bash String? [8 Methods] - LinuxSimply<\/title>\n<meta name=\"description\" content=\"Explore the article to learn different methods to get the first character of string in bash. Hope it will help in efficient bash scripting.\" \/>\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\/get-first-character\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Get the First Character from Bash String? [8 Methods] - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"Explore the article to learn different methods to get the first character of string in bash. Hope it will help in efficient bash scripting.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/afia.zahin.7796?mibextid=ZbWKwL\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-14T08:21:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-31T06:20:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/Bash-1.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=\"Afia Zahin Oishi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Afia Zahin Oishi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 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\\\/get-first-character\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/get-first-character\\\/\"},\"author\":{\"name\":\"Afia Zahin Oishi\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/2afb5d809ba746cd33b77eb2639949d7\"},\"headline\":\"How to Get the First Character from Bash String? [8 Methods]\",\"datePublished\":\"2024-03-14T08:21:58+00:00\",\"dateModified\":\"2024-03-31T06:20:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/get-first-character\\\/\"},\"wordCount\":2202,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/get-first-character\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/Bash-1.png\",\"keywords\":[\"string manipulation\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/get-first-character\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/get-first-character\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/get-first-character\\\/\",\"name\":\"How to Get the First Character from Bash String? [8 Methods] - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/get-first-character\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/get-first-character\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/Bash-1.png\",\"datePublished\":\"2024-03-14T08:21:58+00:00\",\"dateModified\":\"2024-03-31T06:20:33+00:00\",\"description\":\"Explore the article to learn different methods to get the first character of string in bash. Hope it will help in efficient bash scripting.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/get-first-character\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/get-first-character\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/get-first-character\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/Bash-1.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/Bash-1.png\",\"width\":400,\"height\":400,\"caption\":\"An overview on how to get the first character of string in bash\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/get-first-character\\\/#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 Get the First Character from Bash String? [8 Methods]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"name\":\"LinuxSimply\",\"description\":\"All About Linux\",\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/linuxsimply.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\",\"name\":\"LinuxSimply\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"width\":355,\"height\":48,\"caption\":\"LinuxSimply\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/2afb5d809ba746cd33b77eb2639949d7\",\"name\":\"Afia Zahin Oishi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Afia-Zahin-Oishi-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Afia-Zahin-Oishi-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Afia-Zahin-Oishi-II-96x96.png\",\"caption\":\"Afia Zahin Oishi\"},\"description\":\"Assalamualaikum, I am Afia Zahin, completed my graduation in Biomedical Engineering from Bangladesh University of Engineering and Technology, currently working as a Linux Content Developer Executive at SOFTEKO. A high achieving professional with a strong work ethic and able to work in a team in order to consistently achieve my goal and build my skillset. Able to handle difficult problems with patience and swift decision-making. Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/afia.zahin.7796?mibextid=ZbWKwL\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/afia-zahin-oishi-5b4616288\\\/\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/oishi\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Get the First Character from Bash String? [8 Methods] - LinuxSimply","description":"Explore the article to learn different methods to get the first character of string in bash. Hope it will help in efficient bash scripting.","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\/get-first-character\/","og_locale":"en_US","og_type":"article","og_title":"How to Get the First Character from Bash String? [8 Methods] - LinuxSimply","og_description":"Explore the article to learn different methods to get the first character of string in bash. Hope it will help in efficient bash scripting.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/afia.zahin.7796?mibextid=ZbWKwL","article_published_time":"2024-03-14T08:21:58+00:00","article_modified_time":"2024-03-31T06:20:33+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/Bash-1.png","type":"image\/png"}],"author":"Afia Zahin Oishi","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Afia Zahin Oishi","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/"},"author":{"name":"Afia Zahin Oishi","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/2afb5d809ba746cd33b77eb2639949d7"},"headline":"How to Get the First Character from Bash String? [8 Methods]","datePublished":"2024-03-14T08:21:58+00:00","dateModified":"2024-03-31T06:20:33+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/"},"wordCount":2202,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/Bash-1.png","keywords":["string manipulation"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/","name":"How to Get the First Character from Bash String? [8 Methods] - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/Bash-1.png","datePublished":"2024-03-14T08:21:58+00:00","dateModified":"2024-03-31T06:20:33+00:00","description":"Explore the article to learn different methods to get the first character of string in bash. Hope it will help in efficient bash scripting.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/Bash-1.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/Bash-1.png","width":400,"height":400,"caption":"An overview on how to get the first character of string in bash"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/#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 Get the First Character from Bash String? [8 Methods]"}]},{"@type":"WebSite","@id":"https:\/\/linuxsimply.com\/#website","url":"https:\/\/linuxsimply.com\/","name":"LinuxSimply","description":"All About Linux","publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxsimply.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxsimply.com\/#organization","name":"LinuxSimply","url":"https:\/\/linuxsimply.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","width":355,"height":48,"caption":"LinuxSimply"},"image":{"@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/2afb5d809ba746cd33b77eb2639949d7","name":"Afia Zahin Oishi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Afia-Zahin-Oishi-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Afia-Zahin-Oishi-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Afia-Zahin-Oishi-II-96x96.png","caption":"Afia Zahin Oishi"},"description":"Assalamualaikum, I am Afia Zahin, completed my graduation in Biomedical Engineering from Bangladesh University of Engineering and Technology, currently working as a Linux Content Developer Executive at SOFTEKO. A high achieving professional with a strong work ethic and able to work in a team in order to consistently achieve my goal and build my skillset. Able to handle difficult problems with patience and swift decision-making. Read Full Bio","sameAs":["https:\/\/www.facebook.com\/afia.zahin.7796?mibextid=ZbWKwL","https:\/\/www.linkedin.com\/in\/afia-zahin-oishi-5b4616288\/"],"url":"https:\/\/linuxsimply.com\/author\/oishi\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/38628","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\/314907"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=38628"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/38628\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/38631"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=38628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=38628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=38628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}