{"id":27553,"date":"2023-11-20T06:59:06","date_gmt":"2023-11-20T06:59:06","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=27553"},"modified":"2024-03-27T04:31:44","modified_gmt":"2024-03-27T04:31:44","slug":"bash-if-regex","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/","title":{"rendered":"How to Use Regex in Bash If Condition? [4 Cases]"},"content":{"rendered":"<p style=\"text-align: justify;\">Regex or regular expression is a <strong>sequence of characters<\/strong> that specifies a search pattern. Matching with regex can be done by incorporating regex with an <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/\" target=\"_blank\" rel=\"noopener\"><strong>if <\/strong><strong>statement<\/strong><\/a> that involves comparing a string against a pattern determined by the regular expression. In such circumstances, regex is used within the condition of the \u2018if statement\u2019. This operation is hugely used in checking the validity of input, parsing text from big strings, extracting necessary data, and searching.<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/bash-if-regex-scripts.zip\" class=\"su-button su-button-style-default\" style=\"color:#000000;background-color:#E8F9FF;border-color:#bac8cc;border-radius:56px\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"color:#000000;padding:8px 26px;font-size:20px;line-height:30px;border-color:#effbff;border-radius:56px;text-shadow:0px 0px 0px #000000\"><i class=\"sui sui-download\" style=\"font-size:20px;color:#1AA1D6\"><\/i>  Practice Files to Work with Regex in Bash if Condition<\/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\/conditional-statements\/if-else\/regex\/#What_is_Regex\" >What is Regex?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/#4_Cases_of_Regex_Use_in_Bash_If_Condition\" >4 Cases of Regex Use in Bash If Condition<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/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-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/#2_Using_Regex_in_Case-Insensitive_Matching\" >2. Using Regex in Case-Insensitive Matching<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/#3_Using_a_Negated_Regex_in_If_Condition\" >3. 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-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/#4_Using_Regex_with_Variables_in_If_Condition\" >4. Using Regex with Variables in If Condition<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/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-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/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-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/#What_is_the_use_of_regex_if_statement_in_Bash\" >What is the use of regex if statement in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/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-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/#How_do_you_check_if_a_string_is_empty_in_bash\" >How do you check if a string is empty in bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/#What_is_a_pattern_in_regular_expression\" >What is a pattern in regular expression?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"What_is_Regex\"><\/span>What is Regex?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Regex or Regex matching is an operation to look for <strong>similarity between the specified pattern and target string<\/strong>. The pattern is usually a combination of characters and special characters. There are some metacharacters in regex that carry special meaning. Some of these characters are:<\/p>\n<ol>\n<li><strong>\u2018*\u2019:<\/strong> Matches one or more occurrences of the preceding character or group.<\/li>\n<li><strong>\u2018.\u2019(Dot): <\/strong>Matches any single character.<\/li>\n<li><strong>\u2018^\u2019:<\/strong> Anchors the regex at the beginning of the line.<\/li>\n<li><strong>\u2018$\u2019:<\/strong> Anchors the regex at the end of the line.<\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"4_Cases_of_Regex_Use_in_Bash_If_Condition\"><\/span>4 Cases of Regex Use in Bash If Condition<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Regex can be used to find a specific file extension, match a substring within a string irrespective of case, or use any kind of negated regex to find anything except the original string. In the following section, I will discuss 4 use cases of regex within the \u2018if condition\u2019 in bash:<\/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;\">Regular expression or regex is an approach to accomplish some special task by giving expression-based commands. For example, you can search for a specific type of file using the following syntax: <code class=\"\" data-line=\"\">if [[ $file =~ \\.(extension)$ ]]<\/code>. Here the <strong>. (dot)<\/strong> matches any single character before the specified extension with dot.<\/p>\n<p>For a practical approach check out the below script to find if a file is with the <strong>.txt extension<\/strong>:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nfilename=&quot;filex.txt&quot;\n\nif [[ &quot;$filename&quot; =~ \\.txt$ ]]; then\necho &quot;Filename has a .txt extension&quot;\nelse\necho &quot;Filename does not have a .txt extension&quot;\nfi<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>After assigning the filename to the filename variable, the <code class=\"\" data-line=\"\">if [[ &quot;$filename&quot; =~ \\.txt$ ]]<\/code> command looks for a match of the .txt inside the <strong>filename<\/strong> variable. Then print a message according to the return status of the if condition.<\/p>\n<\/div><\/div>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-27555 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/1-extensiton-match.png\" alt=\"The .txt file extension found in the filename.\" width=\"604\" height=\"194\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/1-extensiton-match.png 604w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/1-extensiton-match-300x96.png 300w\" sizes=\"(max-width: 604px) 100vw, 604px\" \/>The script has found a .txt extension inside the <strong>filename<\/strong>.<\/p>\n<div class=\"su-note\"  style=\"border-color:#cedfe2;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#E8F9FC;border-color:#ffffff;color:#333333;\"><strong>Note:<\/strong> To find multiple extensions for example .txt and .csv together inside the filename, modify the condition inside the if condition to <code class=\"\" data-line=\"\">if [[ $filename =~ \\.(txt|csv)$ ]]<\/code>. Here the txt and the and csv extension are piped together and passed after the . (dot) character. <\/div><\/div>\n<h3><span class=\"ez-toc-section\" id=\"2_Using_Regex_in_Case-Insensitive_Matching\"><\/span>2. Using Regex in Case-Insensitive Matching<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Case-insensitive matching means looking for a match irrespective of the case. First, execute the shopt command with option -s nocasematch to enable case-insensitive matching. Then to perform the matching operation of the pattern inside the variable_name use the syntax: <code class=\"\" data-line=\"\">$variable_name =~ ^pattern$<\/code> as the condition inside the if statement.<\/p>\n<p>Here is the bash script on how to find case insensitive match:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nname=&quot;Susmit&quot;\n\nshopt -s nocasematch\n\nif [[ $name =~ ^susmit$ ]]; then\necho &quot;The name is Susmit (case-insensitive).&quot;\nelse\necho &quot;The name is not Susmit (case-insensitive).&quot;\nfi<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The <code class=\"\" data-line=\"\">shopt -s nocasematch<\/code> command enables case-insensitive search. After that the <code class=\"\" data-line=\"\">$name =~ ^susmit$<\/code> inside the if condition looks for a match inside the name variable.<\/p>\n<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-27556 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/2.-case-insensitive.png\" alt=\"Case insensitive match of susmit is found.\" width=\"621\" height=\"184\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/2.-case-insensitive.png 621w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/2.-case-insensitive-300x89.png 300w\" sizes=\"(max-width: 621px) 100vw, 621px\" \/>A case-insensitive match has been found between the assigned pattern and the string inside the name variable.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Using_a_Negated_Regex_in_If_Condition\"><\/span>3. Using a Negated Regex in If Condition<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The negated regex matches everything except the original pattern. To achieve this, the syntax used inside the if condition for such regex is: <code class=\"\" data-line=\"\">if ! [[ &quot;$string_where_to_search&quot; =~ [original pattern] ]];<\/code> For instance, to verify if a string contains any number, inside the string_where_to_search,\u00a0 the necessary command incorporating appropriate negated regex is: <code class=\"\" data-line=\"\">! [[ &quot;$string&quot; =~ [0-9] ]];<\/code>.<\/p>\n<p>Here is the complete bash script to check the existence of a number in a string:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&quot;Hello, world!&quot;\n\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<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>After assigning the string variable the <code class=\"\" data-line=\"\">if ! [[ &quot;$string&quot; =~ [0-9] ]];<\/code> condition checks whether the string variable contains any number. Finally, prints a message according to the result.<\/p>\n<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-27557 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/3.-number-matchig.png\" alt=\"No digit is found inside the string using the regex in if condition.\" width=\"653\" height=\"222\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/3.-number-matchig.png 653w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/3.-number-matchig-300x102.png 300w\" sizes=\"(max-width: 653px) 100vw, 653px\" \/>The bash script does not find any number or digit in the string.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_Using_Regex_with_Variables_in_If_Condition\"><\/span>4. Using Regex with Variables in If Condition<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Regex can be saved in a variable and this variable can be used inside the condition of the if clause by following the syntax:\u00a0 <code class=\"\" data-line=\"\">&quot;string&quot; =~ $pattern<\/code>. Now I will save a pattern inside a variable using <code class=\"\" data-line=\"\">variable_name=&quot;pattern&quot;<\/code> and then use this variable inside the condition of an if clause following the syntax: <code class=\"\" data-line=\"\">&quot;hello&quot; =~ $variable_name.<\/code><\/p>\n<p>Here is the bash script to achieve this:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\npattern=&quot;[a-z]+&quot;\n\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<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The pattern=&#8221;[a-z]+&#8221; command assigned a pattern to the pattern variable. After that, the &#8220;hello&#8221; =~ $pattern condition inside the if clause looks for a match of pattern inside the \u201chello\u201d. Finally prints a message according to the search result.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-27558 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/4.png\" alt=\"Match found between the pattern saved inside the variable and the main string using regex in if condition.\" width=\"605\" height=\"183\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/4.png 605w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/4-300x91.png 300w\" sizes=\"(max-width: 605px) 100vw, 605px\" \/>See, the script has found a match between the string and the assigned pattern inside the variable.<\/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;\">To conclude, regex is a vital tool for searching the intended pattern within the string utilizing the if condition in Bash. Here in this article, I have covered 4 different cases of using regex in if conditions. If you have any queries or questions regarding this article please comment below. Happy Scripting!<\/p>\n<h2><span class=\"ez-toc-section\" id=\"People_Also_Ask\"><\/span>People Also Ask<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Why_use_regex\"><\/span>Why use regex?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The regular expressions (regex) are especially helpful for the filtering purpose. To make a filter more specific or more general, regular expressions are used which are collections of characters that specify a pattern of text to be matched.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_use_of_regex_if_statement_in_Bash\"><\/span>What is the use of regex if statement in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The if statement of the bash script can match text patterns with regex using <strong>=~<\/strong> and double square brackets <strong>[[ ]]<\/strong>. To look for patterns that don&#8217;t match, you can also utilize negative regexes. Variables can be used to hold regex patterns for usage in if statements. Moreover, regex is an effective tool for pattern matching in Bash because of these qualities.<\/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>To compare strings with regular expressions in Bash, use the <strong>\u201cgrep\u201d<\/strong> command with the <strong>\u201c-E\u201d <\/strong>(extended regex) option. Sample script is below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&quot;Linux Simply&quot;\nif echo &quot;$string&quot; | grep -E &quot;^Linux&quot;; then\necho &quot;Match found&quot;\nelse\necho &quot;Match not found&quot;\nfi<\/code><\/pre>\n<p>If the \u201cgrep\u201d command searches a string for a pattern and returns a success exit code 0 if the pattern is found and a failure exit code 1 if the pattern is not found.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_you_check_if_a_string_is_empty_in_bash\"><\/span>How do you check if a string is empty in bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To check if a string is empty in a Bash script, use the -z conditional which returns 0 if the length of the string is 0 and 1 if it is greater than 0.<\/p>\n<p>Sample script for this purpose is below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nVar=&quot;hello&quot;\n\nif [[-z &quot;$Var&quot;]]; then\necho &quot;Var is empty&quot;\nelse\necho &quot;Var is not empty&quot;\nfi<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"What_is_a_pattern_in_regular_expression\"><\/span>What is a pattern in regular expression?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">A regular expression is a pattern that an input text&#8217;s regular expression engine looks for matches in. A pattern is made up of one or more structures, operators, or literal characters.<script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"Why use regex?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"The regular expressions (regex) are especially helpful for the filtering purpose. To make a filter more specific or more general, regular expressions are used which are collections of characters that specify a pattern of text to be matched.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is the use of regex if statement in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"The if statement of the bash script can match text patterns with regex using =~ and double square brackets [[ ]]. To look for patterns that don't match, you can also utilize negative regexes. Variables can be used to hold regex patterns for usage in if statements. Moreover, regex is an effective tool for pattern matching in Bash because of these qualities.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is a pattern in regular expression?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"A regular expression is a pattern that an input text's regular expression engine looks for matches in. A pattern is made up of one or more structures, operators, or literal characters.\"\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\/conditional-statements\/if-else\/if-variable-exists\/\" target=\"_blank\" rel=\"noopener\">Check If a Variable Exists in Bash If Statement<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-argument-exists\/\" target=\"_blank\" rel=\"noopener\">Check If An Input Argument Exists in Bash [4 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-contains-string\/\" target=\"_blank\" rel=\"noopener\">Check If a String Contains Substring, Number &amp; Letter in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-string-not-empty\/\" target=\"_blank\" rel=\"noopener\">Check If a String is Empty or Not in Bash [5 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/compare-strings\/\" target=\"_blank\" rel=\"noopener\">How to Compare Strings in Bash With if Statement [6 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/compare-numbers\/\" target=\"_blank\" rel=\"noopener\">How to Compare Numbers in Bash With If Statement [2 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-in-array\/\" target=\"_blank\" rel=\"noopener\">Check If Array Contains an Element in Bash [6 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-else-one-line\/\" target=\"_blank\" rel=\"noopener\">Bash if-else Condition in One-Line [2 Ways]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/grep\/\" target=\"_blank\" rel=\"noopener\">Using grep in if Statement in Bash [4 Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/rm-if-exists\/\" target=\"_blank\" rel=\"noopener\">Bash rm If Exists [All Case Explained]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/dollar-sign\/\" target=\"_blank\" rel=\"noopener\">What is \u2018$?\u2019 in the Bash if Statement? [8 Examples]<\/a><\/strong><\/li>\n<\/ul>\n<hr \/>\n<p><strong>&lt;&lt; Go Back to <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;If Else in Bash&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/\" target=\"_blank\" rel=\"noopener\">If Else in Bash<\/a><\/span>\u00a0| <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Conditional Statements&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\/conditional-statements\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/\" target=\"_blank\" rel=\"noopener\">Bash Conditional Statements<\/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;27553&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;2&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;4&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;4\\\/5 - (2 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;How to Use Regex in Bash If Condition? [4 Cases]&quot;,&quot;width&quot;:&quot;113.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 113.5px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            4\/5 - (2 votes)    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Regex or regular expression is a sequence of characters that specifies a search pattern. Matching with regex can be done &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How to Use Regex in Bash If Condition? [4 Cases]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/#more-27553\" aria-label=\"Read more about How to Use Regex in Bash If Condition? [4 Cases]\">Read more<\/a><\/p>\n","protected":false},"author":314894,"featured_media":27559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[167],"class_list":["post-27553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-if-else-statement","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Use Regex in Bash If Condition? [4 Cases] - LinuxSimply<\/title>\n<meta name=\"description\" content=\"bash if regex operation enables the useage of regex in the condition of if statement where specified pattern is searched inside a big string.\" \/>\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\/conditional-statements\/if-else\/regex\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use Regex in Bash If Condition? [4 Cases] - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"bash if regex operation enables the useage of regex in the condition of if statement where specified pattern is searched inside a big string.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/dasgupta.susmit\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-20T06:59:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-27T04:31:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/bash-if-regex-feature-image.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Susmit Das Gupta\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Susmit Das Gupta\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 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\\\/conditional-statements\\\/if-else\\\/regex\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/regex\\\/\"},\"author\":{\"name\":\"Susmit Das Gupta\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/38abbef9a1053d9faec8935dc39f10fa\"},\"headline\":\"How to Use Regex in Bash If Condition? [4 Cases]\",\"datePublished\":\"2023-11-20T06:59:06+00:00\",\"dateModified\":\"2024-03-27T04:31:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/regex\\\/\"},\"wordCount\":1243,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/regex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/bash-if-regex-feature-image.png\",\"keywords\":[\"if else statement\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/regex\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/regex\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/regex\\\/\",\"name\":\"How to Use Regex in Bash If Condition? [4 Cases] - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/regex\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/regex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/bash-if-regex-feature-image.png\",\"datePublished\":\"2023-11-20T06:59:06+00:00\",\"dateModified\":\"2024-03-27T04:31:44+00:00\",\"description\":\"bash if regex operation enables the useage of regex in the condition of if statement where specified pattern is searched inside a big string.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/regex\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/regex\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/regex\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/bash-if-regex-feature-image.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/bash-if-regex-feature-image.png\",\"width\":400,\"height\":400,\"caption\":\"bash if regex\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/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 Conditional Statements\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"If Else in Bash\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"How to Use Regex in Bash If Condition? [4 Cases]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"name\":\"LinuxSimply\",\"description\":\"All About Linux\",\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/linuxsimply.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\",\"name\":\"LinuxSimply\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"width\":355,\"height\":48,\"caption\":\"LinuxSimply\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/38abbef9a1053d9faec8935dc39f10fa\",\"name\":\"Susmit Das Gupta\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Susmit-Das-Gupta-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Susmit-Das-Gupta-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Susmit-Das-Gupta-II-96x96.png\",\"caption\":\"Susmit Das Gupta\"},\"description\":\"Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/dasgupta.susmit\",\"https:\\\/\\\/www.instagram.com\\\/susmit_dasgupta\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/susmitdasgupta\\\/\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/susmit\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Use Regex in Bash If Condition? [4 Cases] - LinuxSimply","description":"bash if regex operation enables the useage of regex in the condition of if statement where specified pattern is searched inside a big string.","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\/conditional-statements\/if-else\/regex\/","og_locale":"en_US","og_type":"article","og_title":"How to Use Regex in Bash If Condition? [4 Cases] - LinuxSimply","og_description":"bash if regex operation enables the useage of regex in the condition of if statement where specified pattern is searched inside a big string.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/dasgupta.susmit","article_published_time":"2023-11-20T06:59:06+00:00","article_modified_time":"2024-03-27T04:31:44+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/bash-if-regex-feature-image.png","type":"image\/png"}],"author":"Susmit Das Gupta","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Susmit Das Gupta","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/"},"author":{"name":"Susmit Das Gupta","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/38abbef9a1053d9faec8935dc39f10fa"},"headline":"How to Use Regex in Bash If Condition? [4 Cases]","datePublished":"2023-11-20T06:59:06+00:00","dateModified":"2024-03-27T04:31:44+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/"},"wordCount":1243,"commentCount":2,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/bash-if-regex-feature-image.png","keywords":["if else statement"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/","name":"How to Use Regex in Bash If Condition? [4 Cases] - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/bash-if-regex-feature-image.png","datePublished":"2023-11-20T06:59:06+00:00","dateModified":"2024-03-27T04:31:44+00:00","description":"bash if regex operation enables the useage of regex in the condition of if statement where specified pattern is searched inside a big string.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/bash-if-regex-feature-image.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/bash-if-regex-feature-image.png","width":400,"height":400,"caption":"bash if regex"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/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 Conditional Statements","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/"},{"@type":"ListItem","position":4,"name":"If Else in Bash","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/"},{"@type":"ListItem","position":5,"name":"How to Use Regex in Bash If Condition? [4 Cases]"}]},{"@type":"WebSite","@id":"https:\/\/linuxsimply.com\/#website","url":"https:\/\/linuxsimply.com\/","name":"LinuxSimply","description":"All About Linux","publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxsimply.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxsimply.com\/#organization","name":"LinuxSimply","url":"https:\/\/linuxsimply.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","width":355,"height":48,"caption":"LinuxSimply"},"image":{"@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/38abbef9a1053d9faec8935dc39f10fa","name":"Susmit Das Gupta","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Susmit-Das-Gupta-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Susmit-Das-Gupta-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Susmit-Das-Gupta-II-96x96.png","caption":"Susmit Das Gupta"},"description":"Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio","sameAs":["https:\/\/www.facebook.com\/dasgupta.susmit","https:\/\/www.instagram.com\/susmit_dasgupta\/","https:\/\/www.linkedin.com\/in\/susmitdasgupta\/"],"url":"https:\/\/linuxsimply.com\/author\/susmit\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/27553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/users\/314894"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=27553"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/27553\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/27559"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=27553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=27553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=27553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}