{"id":38864,"date":"2024-03-19T04:49:09","date_gmt":"2024-03-19T04:49:09","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=38864"},"modified":"2024-04-28T04:45:26","modified_gmt":"2024-04-28T04:45:26","slug":"bash-regex","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/","title":{"rendered":"A Complete Guide to Bash Regex"},"content":{"rendered":"<p style=\"text-align: justify;\">In Bash, Regex is the most powerful tool for pattern-matching and manipulation. Regex allows users to search, match, and manipulate text patterns with precision. This article aims to provide a comprehensive tutorial on Bash regex, starting from the fundamentals of regex to practical examples and common challenges encountered when working with regex.<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/Practice_Files_for_Bash_Regex.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 for Bash Regex<\/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\/regex\/#What_is_Bash_Regex\" >What is Bash Regex?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#Types_of_Regex_in_Bash\" >Types of Regex in Bash<\/a><\/li><\/ul><\/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\/regex\/#Metacharacters_in_Bash_Regex\" >Metacharacters in Bash Regex<\/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\/regex\/#How_to_Use_Regex_in_Bash\" >How to Use Regex in Bash?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#1_Using_Regex_Inside_an_If_Clause\" >1. Using Regex Inside an If Clause<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#2_Using_a_Negated_Regex_in_If_Condition\" >2. Using a Negated Regex in If Condition<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#3_Using_Regex_with_Variables_in_If_Condition\" >3. Using Regex with Variables in If Condition<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#4_Using_Regex_with_the_%E2%80%9Csed%E2%80%9D_Command\" >4. Using Regex with the \u201csed\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#5_Using_Regex_with_%E2%80%9Cawk%E2%80%9D_Command\" >5. Using Regex with \u201cawk\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#6_Using_Regex_with_%E2%80%9Cgrep%E2%80%9D_Command\" >6. Using Regex with \u201cgrep\u201d Command<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#How_to_Search_and_Replace_Using_Regex_in_Bash\" >How to Search and Replace Using Regex in Bash?<\/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\/regex\/#How_to_Extract_Domain_Name_From_URL_Using_Regex_in_Bash\" >How to Extract Domain Name From URL Using Regex in Bash?<\/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\/regex\/#How_to_Check_the_Format_of_an_IP_Address_Using_Regex_in_Bash\" >How to Check the Format of an IP Address Using Regex in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#How_to_Check_the_Format_of_an_Email_Using_Regex_in_Bash\" >How to Check the Format of an Email Using Regex in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#How_to_Split_Text_Using_Regex_in_Bash\" >How to Split Text Using Regex in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#How_to_Filter_Text_Using_Regex_in_Bash\" >How to Filter Text Using Regex in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#How_to_Use_Regex_in_Text_Matching\" >How to Use Regex in Text Matching?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#Tips_for_Using_Regex\" >Tips for Using Regex<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#1_Escaping_Special_Characters\" >1. Escaping Special Characters<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#2_Handling_Whitespace\" >2. Handling Whitespace<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#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-23\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#What_is_Regex_in_Bash\" >What is Regex in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#What_are_some_common_regex_metacharacters_in_Bash\" >What are some common regex metacharacters in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-25\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#How_do_I_match_patterns_using_regex\" >How do I match patterns using regex?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-26\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#How_do_I_extract_specific_information_from_a_string_using_regex_in_Bash\" >How do I extract specific information from a string using regex in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-27\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#How_do_I_replace_text_using_regex_in_Bash\" >How do I replace text using regex in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-28\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#How_to_compare_two_strings_in_Bash_regex\" >How to compare two strings in Bash regex?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-29\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#Why_use_regex\" >Why use regex?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-30\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#What_are_some_common_pitfalls_while_using_regex_in_Bash\" >What are some common pitfalls while using regex in Bash?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"What_is_Bash_Regex\"><\/span>What is Bash Regex?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Regex, short for regular expression, is a sequence of characters that define a search pattern for text manipulation or pattern matching. In Bash, regex is used within commands like <strong>grep<\/strong>, <strong>sed<\/strong>, <strong>awk<\/strong>, and even within parameter expansions and conditional expressions. A regular expression comprises one or more elements such as:<\/p>\n<ul>\n<li style=\"text-align: justify;\"><strong>Character set: <\/strong>The character set in regex is a group of specific characters that are treated as their literal meaning. They don\u2019t have any special meaning or function with the regex. For example, if you include the character set <strong>[abc]<\/strong> in a regex, it will match any occurrence of \u2018a\u2019, \u2018b\u2019, or \u2018c\u2019 in the text.<\/li>\n<li style=\"text-align: justify;\"><strong>Anchors: <\/strong>Anchors in regex are special symbols used to mark specific positions in the text where the pattern should match. They don\u2019t represent actual characters but rather indicate where the pattern begins or ends within the text. Two common anchors in regex are <strong>caret (^), <\/strong>and<strong> dollar sign($).<\/strong><\/li>\n<li style=\"text-align: justify;\"><strong>Modifiers: <\/strong>Modifiers in regex are special symbols or characters that expand or narrow the range of text that the regex pattern can match. Some common modifiers in regex include \u2018*\u2019, \u2018+\u2019, \u2018?\u2019,<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Types_of_Regex_in_Bash\"><\/span>Types of Regex in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In Bash, there are<strong> two<\/strong> types of regular expressions (regex) mostly used:<\/p>\n<ol>\n<li style=\"text-align: justify;\"><strong>Basic Regular Expression (BRE):<\/strong> BRE is the default regex type used in many traditional Unix utilities like <strong>grep, sed, <\/strong>and <strong>awk.<\/strong> In BRE, certain metacharacters (such as <code class=\"\" data-line=\"\">?<\/code>, <code class=\"\" data-line=\"\">+<\/code>, <code class=\"\" data-line=\"\">|<\/code>, and <code class=\"\" data-line=\"\">()<\/code>) are treated as literals unless escaped with a backslash <code class=\"\" data-line=\"\">\\<\/code>. BRE is suitable for basic pattern-matching tasks and is widely supported across Unix-based systems.<\/li>\n<li style=\"text-align: justify;\"><strong>Extended Regular Expression (ERE):<\/strong> ERE is an enhanced version of BRE that offers a broader range of metacharacters and features. In ERE, metacharacters such as <code class=\"\" data-line=\"\">?<\/code>, <code class=\"\" data-line=\"\">+<\/code>, <code class=\"\" data-line=\"\">|<\/code>, and <code class=\"\" data-line=\"\">()<\/code> are interpreted with their special meanings by default, without needing to escape them. ERE supports additional features like quantifiers (?, +, {}) and backreferences (\\1, \\2, etc.). Utilities like <strong>grep -E <\/strong>(or <strong>egrep<\/strong>) and <strong>sed -E<\/strong> explicitly support EREs. ERE offers shorter and more expressive regex patterns as compared to BRE. This makes ERE suitable for more complex pattern matching as well as text manipulation tasks.<\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"Metacharacters_in_Bash_Regex\"><\/span>Metacharacters in Bash Regex<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Metacharacters are special characters in regex that enable users to perform powerful operations like pattern matching, substitution, and text manipulation.<\/p>\n<p style=\"text-align: justify;\">Here are some metacharacters commonly used in\u00a0 regex:<\/p>\n<div class=\"su-table su-table-responsive su-table-alternate su-table-fixed\">\n<table style=\"width: 117.517%;height: 1014px\" width=\"641\">\n<tbody>\n<tr style=\"height: 25px\">\n<th style=\"height: 25px\" width=\"102\"><strong>Type<\/strong><\/th>\n<th style=\"height: 25px\" width=\"115\"><strong>Metacharacter<\/strong><\/th>\n<th style=\"height: 25px\" width=\"424\"><strong>Description<\/strong><\/th>\n<\/tr>\n<tr style=\"height: 76px\">\n<td style=\"text-align: center;height: 178px\" rowspan=\"3\" width=\"102\"><strong>Wildcard Characters<\/strong><\/td>\n<td style=\"text-align: center;height: 76px\" width=\"115\"><strong>*<\/strong><\/td>\n<td style=\"text-align: justify;height: 76px\" width=\"424\">Matches zero or more occurrences of the preceding character. For instance, <strong>* .txt<\/strong> matches all the files with the &#8220;.txt&#8221; extension.<\/td>\n<\/tr>\n<tr style=\"height: 51px\">\n<td style=\"text-align: center;height: 51px\" width=\"115\"><strong>? <\/strong><\/td>\n<td style=\"text-align: justify;height: 51px\" width=\"424\">Matches any single character. For example, <strong>file?.txt<\/strong> matches &#8220;file1.txt&#8221;, &#8220;file2.txt&#8221;, etc.<\/td>\n<\/tr>\n<tr style=\"height: 51px\">\n<td style=\"text-align: center;height: 51px\" width=\"115\"><strong>[ ] <\/strong><\/td>\n<td style=\"text-align: justify;height: 51px\" width=\"424\">Matches any single character within the brackets. For instance, <strong>[aeiou] <\/strong>matches any vowel.<\/td>\n<\/tr>\n<tr style=\"height: 51px\">\n<td style=\"text-align: center;height: 102px\" rowspan=\"2\" width=\"102\"><strong>Anchors<\/strong><\/td>\n<td style=\"text-align: center;height: 51px\" width=\"115\"><strong>^<\/strong><\/td>\n<td style=\"text-align: justify;height: 51px\" width=\"424\">Matches the beginning of a line or string. For example, <strong>^hello<\/strong> matches lines that start with &#8220;hello&#8221;.<\/td>\n<\/tr>\n<tr style=\"height: 51px\">\n<td style=\"text-align: center;height: 51px\" width=\"115\"><strong>$ <\/strong><\/td>\n<td style=\"text-align: justify;height: 51px\" width=\"424\">Anchors the regex at the end of the line. For instance, <strong>end$<\/strong> matches lines that end with \u201cend\u201d.<\/td>\n<\/tr>\n<tr style=\"height: 76px\">\n<td style=\"text-align: center;height: 76px\" width=\"102\"><strong>Escaping Character<\/strong><\/td>\n<td style=\"text-align: center;height: 76px\" width=\"115\"><strong>\/<\/strong><\/td>\n<td style=\"text-align: justify;height: 76px\" width=\"424\">Escapes the following character, treating it as a literal character instead of a metacharacter. For instance, \\* matches the asterisk character * literally.<\/td>\n<\/tr>\n<tr style=\"height: 51px\">\n<td style=\"text-align: center;height: 127px\" rowspan=\"2\" width=\"102\"><strong>Quantifiers<\/strong><\/td>\n<td style=\"text-align: center;height: 51px\" width=\"115\"><strong>+ <\/strong><\/td>\n<td style=\"text-align: justify;height: 51px\" width=\"424\">Matcher one or more occurrences of the preceding character.<\/td>\n<\/tr>\n<tr style=\"height: 76px\">\n<td style=\"text-align: center;height: 76px\" width=\"115\"><strong>{}<\/strong><\/td>\n<td style=\"text-align: justify;height: 76px\" width=\"424\">Specifies the exact number of occurrences or a range. For example, a{2} matches &#8220;aa&#8221;, and a{2,4} matches &#8220;aa&#8221;, &#8220;aaa&#8221;, or &#8220;aaaa&#8221;.<\/td>\n<\/tr>\n<tr style=\"height: 76px\">\n<td style=\"text-align: center;height: 76px\" width=\"102\"><strong>Negation <\/strong><\/td>\n<td style=\"text-align: center;height: 76px\" width=\"115\"><strong>[^ ]<\/strong><\/td>\n<td style=\"text-align: justify;height: 76px\" width=\"424\">Negates the character class, and matches any character not listed within the brackets. For instance, <strong>[^0-9]<\/strong> matches any character without a digit<strong>.<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 76px\">\n<td style=\"text-align: center;height: 76px\" width=\"102\"><strong>Grouping<\/strong><\/td>\n<td style=\"text-align: center;height: 76px\" width=\"115\"><strong>()<\/strong><\/td>\n<td style=\"text-align: justify;height: 76px\" width=\"424\">Groups pattern together and allow quantifiers to apply to the entire group. For example, <strong>(abc)+<\/strong> matches\u00a0 &#8220;abc&#8221;, &#8220;abcabc&#8221;, etc.<\/td>\n<\/tr>\n<tr style=\"height: 76px\">\n<td style=\"text-align: center;height: 76px\" width=\"102\"><strong>Alternation<\/strong><\/td>\n<td style=\"text-align: center;height: 76px\" width=\"115\"><strong>|<\/strong><\/td>\n<td style=\"text-align: justify;height: 76px\" width=\"424\">Represents alternation, allowing the matching of one pattern or another. For instance, <strong>cat|dog<\/strong> matches either &#8220;cat&#8221; or &#8220;dog&#8221;.<\/td>\n<\/tr>\n<tr style=\"height: 76px\">\n<td style=\"text-align: center;height: 76px\" width=\"102\"><strong>Special Characters<\/strong><\/td>\n<td style=\"text-align: center;height: 76px\" width=\"115\"><strong>. <\/strong><\/td>\n<td style=\"text-align: justify;height: 76px\" width=\"424\">Matches any single character except newline. For example, a.b matches &#8220;aab&#8221;, &#8220;acb&#8221;, etc., but not &#8220;a\\nb&#8221;.<\/td>\n<\/tr>\n<tr style=\"height: 51px\">\n<td style=\"text-align: center;height: 202px\" rowspan=\"6\" width=\"102\"><strong>Sequence for Common Characters<\/strong><\/td>\n<td style=\"text-align: center;height: 51px\" width=\"115\"><strong>\\w<\/strong><\/td>\n<td style=\"text-align: justify;height: 51px\" width=\"424\">Matches any word character both alphanumeric character and underscore.<\/td>\n<\/tr>\n<tr style=\"height: 51px\">\n<td style=\"text-align: center;height: 51px\" width=\"115\"><strong>\\W<\/strong><\/td>\n<td style=\"text-align: justify;height: 51px\" width=\"424\">Matches any non-word characters except alphanumeric characters and underscore.<\/td>\n<\/tr>\n<tr style=\"height: 25px\">\n<td style=\"text-align: center;height: 25px\" width=\"115\"><strong>\\d<\/strong><\/td>\n<td style=\"text-align: justify;height: 25px\" width=\"424\">Matches any digit.<\/td>\n<\/tr>\n<tr style=\"height: 25px\">\n<td style=\"text-align: center;height: 25px\" width=\"115\"><strong>\\D<\/strong><\/td>\n<td style=\"text-align: justify;height: 25px\" width=\"424\">Matches any non-digit character.<\/td>\n<\/tr>\n<tr style=\"height: 25px\">\n<td style=\"text-align: center;height: 25px\" width=\"115\"><strong>\\s<\/strong><\/td>\n<td style=\"text-align: justify;height: 25px\" width=\"424\">Matches any whitespace character.<\/td>\n<\/tr>\n<tr style=\"height: 25px\">\n<td style=\"text-align: center;height: 25px\" width=\"115\"><strong>\\S<\/strong><\/td>\n<td style=\"text-align: justify;height: 25px\" width=\"424\">Matches any non-whitespace character.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"How_to_Use_Regex_in_Bash\"><\/span>How to Use Regex in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In Bash, Regex can be used in multiple ways for operations like <strong>finding a file extension, matching substring, and finding patterns without<\/strong> the original string. Bash provides several built-in utilities for regex, such as <strong>grep<\/strong>, <strong>sed<\/strong>, and <strong>awk<\/strong>. Here&#8217;s how you can use regex with these utilities:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Using_Regex_Inside_an_If_Clause\"><\/span>1. Using Regex Inside an If Clause<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In Bash, regex within an if statement can be used for finding or matching tasks. For example, to check the extension of the file name, you can use <strong>regex<\/strong> within an <strong>if statement <\/strong>as below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nfilename=&quot;filex.txt&quot;\n\nif [[ &quot;$filename&quot; =~ \\.txt$ ]]; then\n echo &quot;Filename has a .txt extension&quot;\nelse\n echo &quot;Filename does not have a .txt extension&quot;\nfi<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\"> The script checks whether the filename stored in the variable <strong>filename<\/strong> ends with <strong>.txt <\/strong>extension. It uses a conditional statement with the <code class=\"\" data-line=\"\">=~<\/code> operator, which is used for pattern matching with regular expressions. The expression <code class=\"\" data-line=\"\">\\.txt$<\/code> matches the filename ends with <code class=\"\" data-line=\"\">.txt <\/code>extension. The sign <code class=\"\" data-line=\"\">$<\/code> anchors the pattern to the end of the string. <\/div><\/div>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-38866\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/1.-regex-1.png\" alt=\"Using Bash regex inside an IF clause tomatch pattern.\" width=\"601\" height=\"109\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/1.-regex-1.png 601w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/1.-regex-1-300x54.png 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/>The script output shows the result after matching the pattern (.txt) with the file extension.<\/p>\n<div class=\"su-note\"  style=\"border-color:#e5e3d9;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#fffdf3;border-color:#ffffff;color:#333333;\"><strong>Note: <\/strong>You can check the article \u201c<a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/\" target=\"_blank\" rel=\"noopener\"><strong>How to use regex in Bash if clause<\/strong><\/a>\u201d for additional insights.<\/div><\/div>\n<h3><span class=\"ez-toc-section\" id=\"2_Using_a_Negated_Regex_in_If_Condition\"><\/span>2. Using a Negated Regex in If Condition<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Negated regression matches everything except the mentioned pattern. For example, to check whether a variable contains any digits, you can use negated regex within the <strong>if condition<\/strong>. Here\u2019s how the script operates:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&quot;Hello, world!&quot;\n\n#print the string\necho &quot;The string: $string&quot;\necho\n\n#check the string\nif ! [[ &quot;$string&quot; =~ [0-9] ]]; then\necho &quot;String does not contain any digits&quot;\nelse\necho &quot;String contains at least one digit&quot;\nfi<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">The expression <code class=\"\" data-line=\"\">if ! [[ &quot;$string&quot; =~ [0-9] ]];<\/code> then initiates a conditional statement. The <code class=\"\" data-line=\"\">=~<\/code> operator enables regular expression pattern matching in Bash. The regular expression <strong>[0-9]<\/strong> targets any single digit from <strong>0<\/strong> to <strong>9<\/strong> within the string. The negation operator <code class=\"\" data-line=\"\">!<\/code>\u00a0inverts the condition, making it <strong>true <\/strong>when no digits are found in the string. <\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-38867\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/2-negated-regex.png\" alt=\"using negated regex in IF clause to check any digit exists in the string.\" width=\"700\" height=\"135\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/2-negated-regex.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/2-negated-regex-300x58.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>The output shows the result after checking whether the <strong>string<\/strong> contains any number using the negated regex.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Using_Regex_with_Variables_in_If_Condition\"><\/span>3. Using Regex with Variables in If Condition<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Patterns in regular expressions can be stored within variables in Bash using the syntax <code class=\"\" data-line=\"\">variable_name=&quot;pattern&quot;<\/code>, allowing them to be utilized within conditions of if-clauses with the format <code class=\"\" data-line=\"\">&quot;string&quot; =~ $variable_name<\/code>. Here&#8217;s an example of pattern matching:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\npattern=&quot;[a-z]+&quot;\n\n#Amtching the pattern with the string\nif [[ &quot;hello&quot; =~ $pattern ]]; then\necho &quot;String matches the regex pattern&quot;\nelse\necho &quot;String does not match the regex pattern&quot;\nfi<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\"> Here, <code class=\"\" data-line=\"\">[a-z]<\/code>\u00a0defines a character class including all lowercase letters from \u2018<strong>a<\/strong>\u2019 to \u2018<strong>z<\/strong>\u2019, and the quantifier \u2018<code class=\"\" data-line=\"\">+<\/code>\u2019 specifies that the preceding character class <strong>[a-z]<\/strong> can occur one or more times in the string. The condition\u00a0<code class=\"\" data-line=\"\">if [[ &quot;hello&quot; =~ $pattern ]]<\/code>\u00a0checks if the string \u201chello\u201d matches the regular expression (regex) defined in the variable <strong>pattern.<\/strong><\/div><\/div>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter wp-image-38868 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/3-regex-with-variables.png\" alt=\"Using Bash regex with variables in IF clause to check pattern.\" width=\"700\" height=\"133\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/3-regex-with-variables.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/3-regex-with-variables-300x57.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>As the string \u201chello\u201d matches with the defined regex pattern <code class=\"\" data-line=\"\">[a-z]+<\/code>, the script outputs the result as shown in the image.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_Using_Regex_with_the_%E2%80%9Csed%E2%80%9D_Command\"><\/span>4. Using Regex with the \u201csed\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\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 powerful utility for performing text transformations on an input stream (a sequence of text) or files. This command can perform text transformation with the help of <strong>regex. <\/strong>Here\u2019s an example of capitalizing each \u2018a\u2019 in a string:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo &quot;apple banana cherry&quot; | sed &#039;s\/a\/A\/g&#039;<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">In this command, <strong>sed<\/strong> command is used with <strong>regex <\/strong>to perform text substitution. The input string &#8220;apple banana cherry&#8221; is piped into <strong>sed<\/strong>, and the <code class=\"\" data-line=\"\">&#039;s\/a\/A\/g&#039;<\/code> expression is used to substitute all occurrences of the lowercase letter &#8220;a&#8221; with the uppercase letter &#8220;A&#8221;. <\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-38869\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/4-regex-with-sed.png\" alt=\"Using regex with the sed command to replace all occurance of &quot;a&quot; with &quot;A&quot;.\" width=\"825\" height=\"95\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/4-regex-with-sed.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/4-regex-with-sed-300x35.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/4-regex-with-sed-768x88.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>Thus the script replaces all the smaller \u201ca\u201d with the capital \u201cA\u201d using <strong>regex<\/strong> with the <strong>sed command<\/strong>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"5_Using_Regex_with_%E2%80%9Cawk%E2%80%9D_Command\"><\/span>5. Using Regex with \u201cawk\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <a href=\"https:\/\/linuxsimply.com\/awk-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>awk command<\/strong><\/a> is a powerful tool for <strong>text processing<\/strong>, <strong>data extraction<\/strong>, and <strong>report generation<\/strong>, offering a concise and flexible syntax for handling structured data. For example, let\u2019s have a text file named <strong>data.txt<\/strong> with the following content:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">apple 10\nbanana 20\ncherry 30<\/code><\/pre>\n<p style=\"text-align: justify;\">To extract lines where the second column contains the number <strong>20<\/strong> using <strong>regex <\/strong>with the <strong>awk<\/strong> command, follow the script below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Extract lines where the second column contains the number &quot;20&quot; from data.txt\nawk &#039;$2 ~ \/20\/ {print}&#039; data.txt<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">In the expression <code class=\"\" data-line=\"\">&#039;$2 ~ \/20\/ {print}&#039;<\/code>, <strong>$2<\/strong> refers to the second field (column) in each line of input, \u201c<code class=\"\" data-line=\"\">~<\/code>\u201d denotes a pattern-matching operation. <code class=\"\" data-line=\"\">\/20\/<\/code> represents the regular expression pattern to match the string. Finally, the <code class=\"\" data-line=\"\">{print}<\/code> specifies the action to be performed where the pattern is met.<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-38870\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/5-regex-with-awk.png\" alt=\"Using regex with &quot;awk&quot; to extract lines from a file.\" width=\"567\" height=\"102\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/5-regex-with-awk.png 567w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/5-regex-with-awk-300x54.png 300w\" sizes=\"(max-width: 567px) 100vw, 567px\" \/>The script prints the line which contains the number \u201c20\u201d as defined in the regular expression.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"6_Using_Regex_with_%E2%80%9Cgrep%E2%80%9D_Command\"><\/span>6. Using Regex with \u201cgrep\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <a href=\"https:\/\/linuxsimply.com\/grep-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>grep command<\/strong><\/a> is a powerful tool used for <strong>searching text files<\/strong> or <strong>input streams <\/strong>for lines that match a specified pattern. The <strong>grep command <\/strong>along with the <strong>regex <\/strong>can search for lines containing a specific pattern.<\/p>\n<p style=\"text-align: justify;\">Let&#8217;s say we have a file in the current directory called example.txt with the following contents:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">apple\nbanana\ngrape\napricot\nwatermelon<\/code><\/pre>\n<p style=\"text-align: justify;\">Now, the following <strong>grep<\/strong> command will search for apple and apricot with regex to in the file :<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">grep &#039;apple\\|apricot&#039; example.txt<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-38871\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/6-regex-grep.png\" alt=\"Using regex with &quot;grep&quot; command to search in a file.\" width=\"700\" height=\"94\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/6-regex-grep.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/6-regex-grep-300x40.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>The output shows the result after searching the expression as defined in the regex.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_Search_and_Replace_Using_Regex_in_Bash\"><\/span>How to Search and Replace Using Regex in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Regular expressions can be utilized to locate particular patterns or substrings within a given text. Subsequently, they can substitute the identified text or pattern with a designated replacement string. For example, to search all the vowels within a text and replace them with a special character <code class=\"\" data-line=\"\">#<\/code><strong>, <\/strong>check the following script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Input string\ninput=&quot;The quick brown fox jumps over the lazy dog.&quot;\n\n# Perform search and replace using regex\noutput=$(echo &quot;$input&quot; | sed &#039;s\/[aeiou]\/#\/g&#039;)\n\n# Print the modified string\necho &quot;$output&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">This script uses the <strong>sed command <\/strong>with a regular expression to perform a search and replace operation. Specifically, it replaces all occurrences of lowercase vowels (a, e, i, o, u) with the <code class=\"\" data-line=\"\">#<\/code> character. The regular expression<strong> [aeiou]<\/strong> matches any single lowercase vowel, and the sed command&#8217;s <code class=\"\" data-line=\"\">s\/\/\/<\/code> syntax replaces each match with <code class=\"\" data-line=\"\">#<\/code>.<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-38872\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/7-search-and-replace.png\" alt=\"Search and replace a pattern within a string using regex.\" width=\"825\" height=\"195\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/7-search-and-replace.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/7-search-and-replace-300x71.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/7-search-and-replace-768x182.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The output shows the modified string after replacing all the <strong>vowels <\/strong>with the \u201c#\u201d in the input string using regex.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_Extract_Domain_Name_From_URL_Using_Regex_in_Bash\"><\/span>How to Extract Domain Name From URL Using Regex in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Extracting a domain name from a URL involves identifying and isolating the main part of the URL that represents the domain. This typically means removing any scheme (like <strong>\u201chttp:\/\/\u201d or \u201chttps:\/\/\u201d<\/strong>) and any path or query parameters. To extract the domain name from a URL, use <code class=\"\" data-line=\"\">if [[ &quot;$url&quot; =~ ^https?:\/\/([^\/]+) ]]; then domain=\u201d${BASH_REMATCH[1]}\u201d<\/code> syntax.<\/p>\n<p>Check the following script to extract the domain name from the URL:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# URL\nurl=&quot;https:\/\/www.example.com\/path\/to\/page?param=value&quot;\n\n# Extract domain using regex\nif [[ &quot;$url&quot; =~ ^https?:\/\/([^\/]+) ]]; then\ndomain=&quot;${BASH_REMATCH[1]}&quot;\necho &quot;Domain name: $domain&quot;\nelse\necho &quot;Invalid URL&quot;\nfi<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">The script uses regex expression <code class=\"\" data-line=\"\">[[ &quot;$url&quot; =~ ^https?:\/\/([^\/]+) ]]<\/code> which checks the <strong>URL <\/strong>\u00a0starts with either <strong>\u201chttp:\/\/\u201d <\/strong>or <strong>\u201chttps:\/\/\u201d<\/strong> followed by the domain part. The domain part is captured using the (<code class=\"\" data-line=\"\">[^\/]+<\/code>) which matches one or more characters that are not forward slashes <strong>(\/). <\/strong>\u00a0In the script assigns the matched domain name to the domain variable using <code class=\"\" data-line=\"\">${BASH_REMATCH[1]}<\/code>, which extracts the contents of the first capturing group. <\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-38873\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/8-extract-domain-name-from-URL.png\" alt=\"Extract domain name from URL using REgex in Bash.\" width=\"825\" height=\"116\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/8-extract-domain-name-from-URL.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/8-extract-domain-name-from-URL-300x42.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/8-extract-domain-name-from-URL-768x108.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The script outputs the extracted URL from the email address by following the defined regex pattern.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_Check_the_Format_of_an_IP_Address_Using_Regex_in_Bash\"><\/span>How to Check the Format of an IP Address Using Regex in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">To check the format of an <strong>IP address <\/strong>using regex in Bash, you can construct a regex pattern that matches valid IP addresses. Here&#8217;s an example of a Bash script to perform this check:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# IP address\nip=&quot;192.168.277.1&quot;\necho &quot;The ip address: $ip&quot;\necho\n\n# Regex pattern for IP address format\nregex=&#039;^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-&gt;\n\n# Check if IP address matches the regex pattern\nif [[ $ip =~ $regex ]]; then\necho &quot;Valid IP address&quot;\nelse\necho &quot;Invalid IP address&quot;\nfi<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\"> The script sets a specific IP address, <code class=\"\" data-line=\"\">192.168.277.1<\/code> into the <strong>IP<\/strong> variable. A <code class=\"\" data-line=\"\">regex<\/code> pattern is defined to match the format of a valid IPv4 address.\u00a0 The <code class=\"\" data-line=\"\">regex<\/code> used in the script ensures that there are 4 octets consisting of digits in the IP address and each octet is within the range <strong>0-255<\/strong>. The script then checks if the IP address stored in the IP variable matches the regex pattern using the conditional expression <code class=\"\" data-line=\"\">[[ $ip =~ $regex ]]<\/code>.<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-38874\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/9-check-format-of-an-ip-address.png\" alt=\"Check the fomrat of an IP address using Bash regex.\" width=\"684\" height=\"137\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/9-check-format-of-an-ip-address.png 684w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/9-check-format-of-an-ip-address-300x60.png 300w\" sizes=\"(max-width: 684px) 100vw, 684px\" \/>The script identifies the IP address as invalid because the third octet, represented as 277, falls outside the permissible range of 0 to 255.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_Check_the_Format_of_an_Email_Using_Regex_in_Bash\"><\/span>How to Check the Format of an Email Using Regex in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">To check the format of an email using regex in Bash, you can construct a regex pattern that matches valid email addresses. A regex of checking email format is <code class=\"\" data-line=\"\">[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$<\/code><\/p>\n<p style=\"text-align: justify;\">Check the following script to use the regex in email format checking:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nread -p &quot;Enter email: &quot; email\n\n#regex pattern for email address format\nregex=&#039;^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$&#039;\n\nif [[ &quot;$email&quot; =~ $regex]]; then\necho &quot;Valid email address&quot;\nelse\necho &quot;Invalid email address&quot;\nfi<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">The Bash script prompts the user to enter an email address which is stored in the variable <strong>email. <\/strong>The regex pattern <code class=\"\" data-line=\"\">^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$<\/code> is composed of two main components: the local part and the domain part of the email address. The local part, preceding the &#8220;@&#8221; symbol, allows for letters, numbers, and certain special characters such as &#8220;.&#8221;, &#8220;_&#8221;, &#8220;%&#8221;, &#8220;+&#8221;, and &#8220;-&#8220;. The domain part, following the &#8220;@&#8221; symbol, permits letters, numbers, and hyphens, and it mandates that it ends with a period followed by at least two letters (e.g., &#8220;.com&#8221;, &#8220;.org&#8221;, etc.).<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-38875\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/10-email-format-check.png\" alt=\"Check the format of email address using Bash regex.\" width=\"700\" height=\"103\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/10-email-format-check.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/10-email-format-check-300x44.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>The script finds the email address invalid as the email address doesn\u2019t contain the symbol \u201c@\u201d.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_Split_Text_Using_Regex_in_Bash\"><\/span>How to Split Text Using Regex in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Splitting text using regex involves breaking a string into smaller parts based on a defined pattern or delimiter.\u00a0 You can use regex with functions like <strong>grep<\/strong>, <strong>sed<\/strong>, or <strong>awk <\/strong>to split text.<\/p>\n<p style=\"text-align: justify;\">To split text using regular expression (regex) along with the <strong>grep <\/strong>command in Bash, check the following script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\ntext=&quot;apple,banana,orange,grape&quot;\ndelimiter=&quot;,&quot;\n\n#print the string\necho &quot;The text:&quot;\necho $text\necho\necho &quot;The text after splitting&quot;\n\n# Splitting the text using grep and regex\necho &quot;$text&quot; | grep -oE &quot;[^$delimiter]+&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">The string <strong>text<\/strong> contains comma-separated values. The <strong>grep<\/strong> command with the <code class=\"\" data-line=\"\">-oE<\/code> option is used to match and output substrings that don\u2019t contain the specific delimiter. The regex pattern <code class=\"\" data-line=\"\">[^$delimiter]+<\/code> \u00a0matches one or more characters that are not the specified delimiter.<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-38876\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/11-spliting-using-regex.png\" alt=\"Split text using regex in Bash.\" width=\"637\" height=\"300\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/11-spliting-using-regex.png 637w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/11-spliting-using-regex-300x141.png 300w\" sizes=\"(max-width: 637px) 100vw, 637px\" \/>Thus the script splits the substring from the comma-separated string using a regular expression (regex).<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_Filter_Text_Using_Regex_in_Bash\"><\/span>How to Filter Text Using Regex in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Filtering text using regex involves selecting or excluding lines or blocks of text based on a specified pattern. To filter text, use the <strong>grep<\/strong> command with a regex pattern followed by the filename. For example, <code class=\"\" data-line=\"\">grep \u201capple\u201d myfile.txt<\/code> filters all the lines that contain apple in myfile.txt.\u00a0 Here\u2019s a simple example:<\/p>\n<p style=\"text-align: justify;\">Let\u2019s take a text file <strong>data.txt<\/strong> with the following content:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">apple 100\nbanana 150\norange 200\napple 50\ngrape 250\napple 60<\/code><\/pre>\n<p>Check the following script to filter the lines from the <strong>data.txt<\/strong> file that contain the text \u201capple\u201d:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Text file with some data\nfile=&quot;data.txt&quot;\n\n# Filter lines containing &quot;apple&quot; using grep\ngrep &quot;apple&quot; &quot;$file&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">The script uses the <strong>grep<\/strong> command to filter lines from the file <strong>data.txt<\/strong> that contain the word <code class=\"\" data-line=\"\">apple<\/code>.<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-38877\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/12-Filetring-using-regex.png\" alt=\"Filter text using regex in Bash.\" width=\"692\" height=\"139\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/12-Filetring-using-regex.png 692w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/12-Filetring-using-regex-300x60.png 300w\" sizes=\"(max-width: 692px) 100vw, 692px\" \/>The script output displays the filtered lines from the <strong>data.txt<\/strong> files using the regular expression (regex).<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_Use_Regex_in_Text_Matching\"><\/span>How to Use Regex in Text Matching?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">You can use regex within an <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/\" target=\"_blank\" rel=\"noopener\"><strong>if statement <\/strong><\/a>for text matching. In this task, two cases can appear: one is case-sensitive, other is case-insensitive text matching. Check the following script to use regex in text matching:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nname=&quot;Watson&quot;\n\nshopt -s nocasematch\n\nif [[ $name =~ ^watson$ ]]; then\necho &quot;The name is Watson (case-insensitive).&quot;\nelse\necho &quot;The name is not Watson (case-insensitive).&quot;\nfi<\/code><\/pre>\n<p style=\"text-align: justify;\"><div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">This script checks for a pattern in the string variable <strong>name <\/strong>in a case-insensitive manner. To enable case-insensitivity, the script uses the <code class=\"\" data-line=\"\">shopt -s nocasematch<\/code> command. The expression\u00a0 <code class=\"\" data-line=\"\">$name =~ ^watson$<\/code> checks if the value of the variable name matches the regular expression <code class=\"\" data-line=\"\">^watson$<\/code>. <\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-38878\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/13-text-matching.png\" alt=\"Using regex in text matching\" width=\"650\" height=\"117\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/13-text-matching.png 650w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/13-text-matching-300x54.png 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/>The output shows the result after matching the pattern with the variable <strong>name<\/strong> using <strong>regex<\/strong> expression.<\/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 make the above operation case-sensitive, remove the command <code class=\"\" data-line=\"\">shopt -s nocasematch<\/code> from the above script.<\/div><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Tips_for_Using_Regex\"><\/span>Tips for Using Regex<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Though regular expression (regex) is powerful, it can present several challenges and issues like escaping special characters, handling white space, etc.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Escaping_Special_Characters\"><\/span>1. Escaping Special Characters<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Special characters in regex patterns, such as <code class=\"\" data-line=\"\">^<\/code>, <code class=\"\" data-line=\"\">$<\/code>, and <code class=\"\" data-line=\"\">\\<\/code>\u00a0need to be properly escaped to match their literal meaning. To handle special characters like <code class=\"\" data-line=\"\">*<\/code> in regex patterns, you must escape them with a backslash <code class=\"\" data-line=\"\">\\<\/code> to treat them as literals. Here\u2019s an example of escaping special characters in the <strong>regex pattern<\/strong>:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&#039;The cost is $100&#039;\npattern=&#039;\\$100&#039;\n\nif [[ $string =~ $pattern ]]; then\necho &#039;Match found&#039;\nelse\necho &#039;Match not found&#039;\nfi<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-38879\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/14-escaping-sepcial-character.png\" alt=\"Escaping spcial character when using regex in Bash.\" width=\"700\" height=\"63\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/14-escaping-sepcial-character.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/14-escaping-sepcial-character-300x27.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>In the script, the regex pattern <code class=\"\" data-line=\"\">\\$100<\/code> uses the backslash <code class=\"\" data-line=\"\">\\<\/code> to escape the dollar sign <code class=\"\" data-line=\"\">$<\/code>, ensuring that it is treated as a literal character while pattern matching.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_Handling_Whitespace\"><\/span>2. Handling Whitespace<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Whitespace characters like Space and Tab can lead to unexpected behavior in regex. To handle whitespace issues of regex, you can use the <code class=\"\" data-line=\"\">[:space:]<\/code> character class or the <code class=\"\" data-line=\"\">\\s<\/code>\u00a0metacharacter. Here\u2019s an example to handle whitespace while pattern matching using regex:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&#039;Regex\u00a0\u00a0\u00a0\u00a0\u00a0 in\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 action&#039;\npattern=&#039;Regex in action&#039;\n\n#print the string and the pattern\necho &quot;The string: $string&quot;\necho &quot;The regex pattern: $pattern&quot;\necho\n\nif [[ $string =~ $pattern ]]; then\necho &#039;Match found&#039;\nelse\necho &#039;Match not found&#039;\nfi\n\necho\necho &quot;Handling whitespace&quot;\n\nnew_pattern=&#039;Regex\\s+in\\s+action&#039;\n\necho &quot;The new regex pattern: $new_pattern&quot;\necho\n\nif [[ $string =~ $new_pattern ]]; then\necho &#039;Match found&#039;\nelse\necho &#039;Match not found&#039;\nfi<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-38880\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/15-handling-whitespace.png\" alt=\"Handling whitespace using the metacharacter '\\s' when using regex.\" width=\"684\" height=\"280\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/15-handling-whitespace.png 684w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/15-handling-whitespace-300x123.png 300w\" sizes=\"(max-width: 684px) 100vw, 684px\" \/>The script handles whitespace while matching patterns using the <code class=\"\" data-line=\"\">\\s<\/code> metacharacter to match any whitespace characters. It correctly identifies multiple consecutive whitespaces between words ensuring effective pattern matching.<\/p>\n<p style=\"text-align: justify;\">Alternatively, you can use\u00a0 the <code class=\"\" data-line=\"\">[:space:]<\/code> character class to handle whitespace when working with regex as below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&#039;Regex\u00a0\u00a0\u00a0\u00a0\u00a0 in\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 action&#039;\npattern=&#039;Regex[[:space:]]+in[[:space:]]+action&#039;\n\nif [[ $string =~ $pattern ]]; then\necho &#039;Match found&#039;\nelse\necho &#039;Match not found&#039;\nfi<\/code><\/pre>\n<p style=\"text-align: justify;\">This script also handles whitespace issues by using the <code class=\"\" data-line=\"\">[:space:]<\/code> character class to match any whitespace characters.<\/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, regular expressions (regex) are indispensable tools in the area of any Bash script developer. From string validation to text manipulation, regex helps to tackle a wide range of tasks with precision and efficiency. This article provides a comprehensive overview of regex patterns, explaining their usage and functionality through solving several problems. Hope this article serves to expand your understanding of regular expressions (regex) and enhances your ability to use them effectively in various contexts.<\/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 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"What_is_Regex_in_Bash\"><\/span>What is Regex in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Regex, short for regular expression, is a powerful tool for defining search patterns using character sequences. It simplifies tasks such as string manipulation, user input validation, log file parsing, and text data extraction. With its flexibility and versatility, regex empowers developers to perform complex operations effortlessly.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_are_some_common_regex_metacharacters_in_Bash\"><\/span>What are some common regex metacharacters in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In Bash, the common regex metacharacters include <strong>*<\/strong>, <strong>?<\/strong>, <strong>[ ]<\/strong>, <strong>^<\/strong>, <strong>$<\/strong>, <strong>{ }<\/strong>, <strong>|<\/strong>, <strong>.<\/strong>, and <strong>\\<\/strong>. These metacharacters are fundamental in pattern matching and text manipulation.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_match_patterns_using_regex\"><\/span>How do I match patterns using regex?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">You can match a pattern in a string using the <code class=\"\" data-line=\"\">[[$string =~ $pattern]]<\/code> conditional expression or by using commands like <strong>grep<\/strong>, <strong>sed<\/strong>, or <strong>awk <\/strong>with appropriate <strong>regex patterns<\/strong>.<\/p>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"How_do_I_extract_specific_information_from_a_string_using_regex_in_Bash\"><\/span>How do I extract specific information from a string using regex in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To extract specific information from a string, you can use commands like <strong>grep<\/strong>, <strong>sed<\/strong>, or <strong>awk <\/strong>with regex patterns that match the desired information.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_replace_text_using_regex_in_Bash\"><\/span>How do I replace text using regex in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">You can replace text using commands like <strong>sed<\/strong> or <strong>awk<\/strong> with the <code class=\"\" data-line=\"\">s\/regex\/replacement\/<\/code> syntax, where regex is the pattern to match and replacement is the text to replace it with.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_compare_two_strings_in_Bash_regex\"><\/span>How to compare two strings in Bash regex?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To compare two strings using regular expressions, you can use the <strong>grep <\/strong>command with the <code class=\"\" data-line=\"\">&lt;strong&gt;-E&lt;\/strong&gt;<\/code>\u00a0option that refers to extended regex. Check the following script to compare two strings in Bash regex:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">string1=&quot;Hello&quot;\nstring2=&quot;hello&quot;\n\nif grep -qE &quot;^$string2$&quot; &lt;&lt;&lt; &quot;$string1&quot;; then\necho &quot;Strings are equal&quot;\nelse\necho &quot;Strings are not equal&quot;\nfi<\/code><\/pre>\n<p>Here, grep is employed with the <code class=\"\" data-line=\"\">-q<\/code> option to suppress output and the <code class=\"\" data-line=\"\">-E<\/code> option to enable extended regex. The <code class=\"\" data-line=\"\">^<\/code> and <code class=\"\" data-line=\"\">$<\/code> anchors ensure exact string matching.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Why_use_regex\"><\/span>Why use regex?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Regular expressions are versatile and powerful tools for <strong>pattern matching, validation, text extraction, <\/strong>and<strong> transformation<\/strong>. They provide a flexible way to search for patterns in text data and validate input data against predefined formats. Regex allows for the extraction of specific information or substrings and the manipulation of text data by replacing, rearranging, or formatting it according to specified patterns.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_are_some_common_pitfalls_while_using_regex_in_Bash\"><\/span>What are some common pitfalls while using regex in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Common pitfalls when using regex in Bash include mishandling special characters, improper whitespace management, overlooking multiline input, and creating overly complex patterns that slow down processing. Thorough testing and validation of regex patterns are vital to prevent unexpected errors and ensure the reliability of your scripts. Understanding regex syntax and implementing best practices can help you avoid these pitfalls and enhance the effectiveness of your Bash scripts.<\/p>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"What is Regex in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Regex, short for regular expression, is a powerful tool for defining search patterns using character sequences. It simplifies tasks such as string manipulation, user input validation, log file parsing, and text data extraction. With its flexibility and versatility, regex empowers developers to perform complex operations effortlessly.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How do I match patterns using regex?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"You can match a pattern in a string using the [[$string =~ $pattern]] conditional expression or by using commands like grep, sed, or awk with appropriate regex patterns.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How do I extract specific information from a string using regex in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To extract specific information from a string, you can use commands like grep, sed, or awk with regex patterns that match the desired information.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How do I replace text using regex in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"You can replace text using commands like sed or awk with the s\/regex\/replacement\/ syntax, where regex is the pattern to match and replacement is the text to replace it with.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Why use regex?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Regular expressions are versatile and powerful tools for pattern matching, validation, text extraction, and transformation. They provide a flexible way to search for patterns in text data and validate input data against predefined formats. Regex allows for the extraction of specific information or substrings and the manipulation of text data by replacing, rearranging, or formatting it according to specified patterns.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What are some common pitfalls while using regex in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Common pitfalls when using regex in Bash include mishandling special characters, improper whitespace management, overlooking multiline input, and creating overly complex patterns that slow down processing. Thorough testing and validation of regex patterns are vital to prevent unexpected errors and ensure the reliability of your scripts. Understanding regex syntax and implementing best practices can help you avoid these pitfalls and enhance the effectiveness of your Bash scripts.\"\n    }\n  }]\n}\n<\/script><\/p>\n<p><span style=\"font-size: 18pt; color: #003366;\"><strong>Related Articles<\/strong><\/span><\/p>\n<ul>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/basics\/\" target=\"_blank\" rel=\"noopener\">Bash String Basics<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/operations\/\" target=\"_blank\" rel=\"noopener\">Bash String Operations<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/\" target=\"_blank\" rel=\"noopener\">String Manipulation in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/split-string\/\" target=\"_blank\" rel=\"noopener\">8 Methods to Split String in Bash [With Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/substring\/\" target=\"_blank\" rel=\"noopener\">How to Extract Bash Substring? [5 methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/check-string\/\" target=\"_blank\" rel=\"noopener\">Check String in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/multiline-string\/\" target=\"_blank\" rel=\"noopener\">Bash Multiline String<\/a><\/strong><\/li>\n<\/ul>\n<hr \/>\n<p><strong>&lt;&lt; Go Back to\u00a0<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;38864&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;A Complete Guide to Bash Regex&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            <span class=\"kksr-muted\">Rate this post<\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>In Bash, Regex is the most powerful tool for pattern-matching and manipulation. Regex allows users to search, match, and manipulate &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"A Complete Guide to Bash Regex\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#more-38864\" aria-label=\"Read more about A Complete Guide to Bash Regex\">Read more<\/a><\/p>\n","protected":false},"author":314912,"featured_media":38881,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[189],"class_list":["post-38864","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-bash-string","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>A Complete Guide to Bash Regex - LinuxSimply<\/title>\n<meta name=\"description\" content=\"In Bash, regex is collection of characters to define a pattern which is used to match pattern, extract and validate text, etc.\" \/>\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\/regex\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A Complete Guide to Bash Regex - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"In Bash, regex is collection of characters to define a pattern which is used to match pattern, extract and validate text, etc.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/profile.php?id=100077694755838&amp;mibextid=ZbWKwL\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-19T04:49:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-28T04:45:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/16-Bash-Regex.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Auhona Islam\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Auhona Islam\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/regex\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/regex\\\/\"},\"author\":{\"name\":\"Auhona Islam\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/25a3d16db4655804617c9e0c4a7989fb\"},\"headline\":\"A Complete Guide to Bash Regex\",\"datePublished\":\"2024-03-19T04:49:09+00:00\",\"dateModified\":\"2024-04-28T04:45:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/regex\\\/\"},\"wordCount\":3283,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/regex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/16-Bash-Regex.png\",\"keywords\":[\"bash string\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/regex\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/regex\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/regex\\\/\",\"name\":\"A Complete Guide to Bash Regex - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/regex\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/regex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/16-Bash-Regex.png\",\"datePublished\":\"2024-03-19T04:49:09+00:00\",\"dateModified\":\"2024-04-28T04:45:26+00:00\",\"description\":\"In Bash, regex is collection of characters to define a pattern which is used to match pattern, extract and validate text, etc.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/regex\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/regex\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/regex\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/16-Bash-Regex.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/16-Bash-Regex.png\",\"width\":400,\"height\":400,\"caption\":\"Regular expressions (regex) in Bash.\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/regex\\\/#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\":\"A Complete Guide to Bash Regex\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"name\":\"LinuxSimply\",\"description\":\"All About Linux\",\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/linuxsimply.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\",\"name\":\"LinuxSimply\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"width\":355,\"height\":48,\"caption\":\"LinuxSimply\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/25a3d16db4655804617c9e0c4a7989fb\",\"name\":\"Auhona Islam\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Auhona-Islam-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Auhona-Islam-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Auhona-Islam-II-96x96.png\",\"caption\":\"Auhona Islam\"},\"description\":\"Auhona Islam is a dedicated professional with a background in Electronics and Communication Engineering (ECE) from Khulna University of Engineering &amp; Technology. Graduating in 2023, Auhona is currently excelling in her role as a Linux content developer executive at SOFTEKO to provide a more straightforward route for Linux users. She aims to generate compelling materials for Linux users with her knowledge and skills. She holds her enthusiasm in the realm of Machine Learning (ML), Deep Learning (DL), and Artificial Intelligence (AI). Apart from these, she has a passion for playing instruments and singing. Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/profile.php?id=100077694755838&mibextid=ZbWKwL\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/auhona-islam-429822208\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/auhona\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A Complete Guide to Bash Regex - LinuxSimply","description":"In Bash, regex is collection of characters to define a pattern which is used to match pattern, extract and validate text, etc.","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\/regex\/","og_locale":"en_US","og_type":"article","og_title":"A Complete Guide to Bash Regex - LinuxSimply","og_description":"In Bash, regex is collection of characters to define a pattern which is used to match pattern, extract and validate text, etc.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/profile.php?id=100077694755838&mibextid=ZbWKwL","article_published_time":"2024-03-19T04:49:09+00:00","article_modified_time":"2024-04-28T04:45:26+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/16-Bash-Regex.png","type":"image\/png"}],"author":"Auhona Islam","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Auhona Islam","Est. reading time":"17 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/"},"author":{"name":"Auhona Islam","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/25a3d16db4655804617c9e0c4a7989fb"},"headline":"A Complete Guide to Bash Regex","datePublished":"2024-03-19T04:49:09+00:00","dateModified":"2024-04-28T04:45:26+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/"},"wordCount":3283,"commentCount":3,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/16-Bash-Regex.png","keywords":["bash string"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/","name":"A Complete Guide to Bash Regex - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/16-Bash-Regex.png","datePublished":"2024-03-19T04:49:09+00:00","dateModified":"2024-04-28T04:45:26+00:00","description":"In Bash, regex is collection of characters to define a pattern which is used to match pattern, extract and validate text, etc.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/16-Bash-Regex.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/16-Bash-Regex.png","width":400,"height":400,"caption":"Regular expressions (regex) in Bash."},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/regex\/#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":"A Complete Guide to Bash Regex"}]},{"@type":"WebSite","@id":"https:\/\/linuxsimply.com\/#website","url":"https:\/\/linuxsimply.com\/","name":"LinuxSimply","description":"All About Linux","publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxsimply.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxsimply.com\/#organization","name":"LinuxSimply","url":"https:\/\/linuxsimply.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","width":355,"height":48,"caption":"LinuxSimply"},"image":{"@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/25a3d16db4655804617c9e0c4a7989fb","name":"Auhona Islam","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Auhona-Islam-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Auhona-Islam-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Auhona-Islam-II-96x96.png","caption":"Auhona Islam"},"description":"Auhona Islam is a dedicated professional with a background in Electronics and Communication Engineering (ECE) from Khulna University of Engineering &amp; Technology. Graduating in 2023, Auhona is currently excelling in her role as a Linux content developer executive at SOFTEKO to provide a more straightforward route for Linux users. She aims to generate compelling materials for Linux users with her knowledge and skills. She holds her enthusiasm in the realm of Machine Learning (ML), Deep Learning (DL), and Artificial Intelligence (AI). Apart from these, she has a passion for playing instruments and singing. Read Full Bio","sameAs":["https:\/\/www.facebook.com\/profile.php?id=100077694755838&mibextid=ZbWKwL","https:\/\/www.linkedin.com\/in\/auhona-islam-429822208"],"url":"https:\/\/linuxsimply.com\/author\/auhona\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/38864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/users\/314912"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=38864"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/38864\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/38881"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=38864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=38864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=38864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}