{"id":2468,"date":"2017-01-19T17:15:13","date_gmt":"2017-01-19T15:15:13","guid":{"rendered":"http:\/\/www.systemcodegeeks.com\/?p=2468"},"modified":"2018-01-10T12:26:27","modified_gmt":"2018-01-10T10:26:27","slug":"bash-if-statement-example","status":"publish","type":"post","link":"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/","title":{"rendered":"Bash If &#8211; Statement Example"},"content":{"rendered":"<p>This is an If &#8211; Condition Example. Sometimes you need to specify different courses of action to be taken in a shell script, depending on the success or failure of a command. The if construction allows you to specify such conditions.<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n[ulp id=&#8217;SG9vnnKgRCD7iZxf&#8217;]<br \/>\n&nbsp;<br \/>\nThe most compact syntax of the if command is:<\/p>\n<pre class=\"brush:xml\">if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi\r\n<\/pre>\n<p>The <code>TEST-COMMAND<\/code> list is executed, and if its return status is zero, the <code>CONSEQUENT-COMMANDS<\/code> list is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.<\/p>\n<p>The <code>TEST-COMMAND<\/code> often involves numerical or string comparison tests, but it can also be any command that returns a status of zero when it succeeds and some other status when it fails. Unary expressions are often used to examine the status of a file. If the FILE argument to one of the primaries is of the form <code>\/dev\/fd\/N<\/code>, then file descriptor &#8220;N&#8221; is checked. stdin, stdout and stderr and their respective file descriptors may also be used for tests.<\/p>\n<p>The following table shows an overview of the whole article:<\/p>\n<div class=\"toc\">\n<h3>Table Of Contents<\/h3>\n<dl>\n<dt><a href=\"#if\">1. Expressions used with if<\/a><\/dt>\n<dt><a href=\"#then\">2. Commands following the then statement<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#then_file\">2.1 Checking Files<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dd>\n<dl>\n<dt><a href=\"#then_exit\">2.2 Testing exit status<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dd>\n<dl>\n<dt><a href=\"#then_numeric\">2.3 Numeric comparisons<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dd>\n<dl>\n<dt><a href=\"#then_string\">2.4 String comparisons<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#summary\">3. Summary<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"if\"><\/a>1. Expressions used with if<\/h2>\n<p>The table below contains an overview of the so-called &#8220;primaries&#8221; that make up the <code>TEST-COMMAND<\/code> command or list of commands. These primaries are put between square brackets to indicate the test of a conditional expression.<\/p>\n<table>\n<tbody>\n<tr>\n<th>Primary<\/th>\n<th>Meaning<\/th>\n<\/tr>\n<tr>\n<td>[ -a FILE ]<\/td>\n<td>True if FILE exists.<\/td>\n<\/tr>\n<tr>\n<td>[ -b FILE ]<\/td>\n<td>True if FILE exists and is a block-special file.<\/td>\n<\/tr>\n<tr>\n<td>[ -c FILE ]<\/td>\n<td>True if FILE exists and is a character-special file.<\/td>\n<\/tr>\n<tr>\n<td>[ -d FILE ]<\/td>\n<td>True if FILE exists and is a directory.<\/td>\n<\/tr>\n<tr>\n<td>[ -e FILE ]<\/td>\n<td>True if FILE exists.<\/td>\n<\/tr>\n<tr>\n<td>[ -f FILE ]<\/td>\n<td>True if FILE exists and is a regular file.<\/td>\n<\/tr>\n<tr>\n<td>[ -g FILE ]<\/td>\n<td>True if FILE exists and its SGID bit is set.<\/td>\n<\/tr>\n<tr>\n<td>[ -h FILE ]<\/td>\n<td>True if FILE exists and is a symbolic link.<\/td>\n<\/tr>\n<tr>\n<td>[ -k FILE ]<\/td>\n<td>True if FILE exists and its sticky bit is set.<\/td>\n<\/tr>\n<tr>\n<td>[ -p FILE ]<\/td>\n<td>True if FILE exists and is a named pipe (FIFO).<\/td>\n<\/tr>\n<tr>\n<td>[ -r FILE ]<\/td>\n<td>True if FILE exists and is readable.<\/td>\n<\/tr>\n<tr>\n<td>[ -s FILE ]<\/td>\n<td>True if FILE exists and has a size greater than zero.<\/td>\n<\/tr>\n<tr>\n<td>[ -t FD ]<\/td>\n<td>True if file descriptor FD is open and refers to a terminal.<\/td>\n<\/tr>\n<tr>\n<td>[ -u FILE ]<\/td>\n<td>True if FILE exists and its SUID (set user ID) bit is set.<\/td>\n<\/tr>\n<tr>\n<td>[ -w FILE ]<\/td>\n<td>True if FILE exists and is writable.<\/td>\n<\/tr>\n<tr>\n<td>[ -x FILE ]<\/td>\n<td>True if FILE exists and is executable.<\/td>\n<\/tr>\n<tr>\n<td>[ -O FILE ]<\/td>\n<td>True if FILE exists and is owned by the effective user ID.<\/td>\n<\/tr>\n<tr>\n<td>[ -G FILE ]<\/td>\n<td>True if FILE exists and is owned by the effective group ID.<\/td>\n<\/tr>\n<tr>\n<td>[ -L FILE ]<\/td>\n<td>True if FILE exists and is a symbolic link.<\/td>\n<\/tr>\n<tr>\n<td>[ -N FILE ]<\/td>\n<td>True if FILE exists and has been modified since it was last read.<\/td>\n<\/tr>\n<tr>\n<td>[ -S FILE ]<\/td>\n<td>True if FILE exists and is a socket.<\/td>\n<\/tr>\n<tr>\n<td>[ FILE1 -nt FILE2 ]<\/td>\n<td>True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not.<\/td>\n<\/tr>\n<tr>\n<td>[ FILE1 -ot FILE2 ]<\/td>\n<td>True if FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not.<\/td>\n<\/tr>\n<tr>\n<td>[ FILE1 -ef FILE2 ]<\/td>\n<td>True if FILE1 and FILE2 refer to the same device and inode numbers.<\/td>\n<\/tr>\n<tr>\n<td>[ -o OPTIONNAME ]<\/td>\n<td>True if shell option &#8220;OPTIONNAME&#8221; is enabled.<\/td>\n<\/tr>\n<tr>\n<td>[ -z STRING ]<\/td>\n<td>True of the length if &#8220;STRING&#8221; is zero.<\/td>\n<\/tr>\n<tr>\n<td>[ -n STRING ] or [ STRING ]<\/td>\n<td>True if the length of &#8220;STRING&#8221; is non-zero.<\/td>\n<\/tr>\n<tr>\n<td>[ STRING1 == STRING2 ]<\/td>\n<td>True if the strings are equal. &#8220;=&#8221; may be used instead of &#8220;==&#8221; for strict POSIX compliance.<\/td>\n<\/tr>\n<tr>\n<td>[ STRING1 != STRING2 ]<\/td>\n<td>True if the strings are not equal.<\/td>\n<\/tr>\n<tr>\n<td>[ STRING1 &lt; STRING2 ]<\/td>\n<td>True if &#8220;STRING1&#8221; sorts before &#8220;STRING2&#8221; lexicographically in the current locale.<\/td>\n<\/tr>\n<tr>\n<td>[ STRING1 &gt; STRING2 ]<\/td>\n<td>True if &#8220;STRING1&#8221; sorts after &#8220;STRING2&#8221; lexicographically in the current locale.<\/td>\n<\/tr>\n<tr>\n<td>[ ARG1 OP ARG2 ]<\/td>\n<td>&#8220;OP&#8221; is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if &#8220;ARG1&#8221; is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to &#8220;ARG2&#8221;, respectively. &#8220;ARG1&#8221; and &#8220;ARG2&#8221; are integers.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Expressions may be combined using the following operators, listed in decreasing order of precedence:<\/p>\n<table>\n<tbody>\n<tr>\n<th>Operation<\/th>\n<th>Effect<\/th>\n<\/tr>\n<tr>\n<td>[ ! EXPR ]<\/td>\n<td>True if EXPR is false.<\/td>\n<\/tr>\n<tr>\n<td>[ ( EXPR ) ]<\/td>\n<td>Returns the value of EXPR. This may be used to override the normal precedence of operators.<\/td>\n<\/tr>\n<tr>\n<td>[ EXPR1 -a EXPR2 ]<\/td>\n<td>True if both EXPR1 and EXPR2 are true.<\/td>\n<\/tr>\n<tr>\n<td>[ EXPR1 -o EXPR2 ]<\/td>\n<td>True if either EXPR1 or EXPR2 is true.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The [ (or test) built-in evaluates conditional expressions using a set of rules based on the number of arguments. More information about this subject can be found in the Bash documentation.<\/p>\n<p>Just like the if is closed with fi, the opening square bracket should be closed after the conditions have been listed.<\/p>\n<h2><a name=\"then\"><\/a>2. Commands following the then statement<\/h2>\n<p>The <code>CONSEQUENT-COMMANDS<\/code> list that follows the then statement can be any valid UNIX command, any executable program, any executable shell script or any shell statement, with the exception of the closing fi.<\/p>\n<p>It is important to remember that the then and if are considered to be separated statements in the shell.<\/p>\n<p>Therefore, when issued on the command line, they are separated by a semi-colon. In a script, the different parts of the if statement are usually well-separated.<\/p>\n<h3><a name=\"then_file\"><\/a>2.1 Checking Files<\/h3>\n<p>The following file represents a simple textfile for some tests:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Test1.txt<\/em><\/span><\/p>\n<pre class=\"brush:xml\">1:Andreas\r\n2:Marcus\r\n3:Tom\r\n4:Steve\r\n<\/pre>\n<p>The first example checks for the existence of a file:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>filecheck.sh<\/em><\/span><\/p>\n<pre class=\"brush:xml\">#!\/bin\/bash\r\necho \"Checking...\"\r\nif [ -f Test1.txt ]\r\nthen\r\necho \"File Test1.txt exists.\"\r\nfi\r\necho\r\necho \"...done.\"\r\n<\/pre>\n<figure id=\"attachment_2479\" aria-describedby=\"caption-attachment-2479\" style=\"width: 511px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-2479\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample1.jpg\" alt=\"An If Example for checking files\" width=\"511\" height=\"347\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample1.jpg 511w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample1-300x204.jpg 300w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><\/a><figcaption id=\"caption-attachment-2479\" class=\"wp-caption-text\">An If Example for checking files<\/figcaption><\/figure>\n<h3><a name=\"then_exit\"><\/a>2.2 Testing exit status<\/h3>\n<p>The ? variable holds the exit status of the previously executed command (the most recently completed foreground process).<\/p>\n<p>The following example shows a simple test:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>exitcheck1.sh<\/em><\/span><\/p>\n<pre class=\"brush:xml\">echo \"Reading...\"\r\nls Test*.txt\r\necho\r\n\r\necho \"Checking...\"\r\nif [ $? -eq 0 ]\r\nthen\r\necho 'Success!'\r\nfi\r\necho\r\necho \"...done.\"\r\n<\/pre>\n<figure id=\"attachment_2480\" aria-describedby=\"caption-attachment-2480\" style=\"width: 511px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample2.jpg\"><img decoding=\"async\" class=\"size-full wp-image-2480\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample2.jpg\" alt=\"An If Example, which checks the Exit State of a Command\" width=\"511\" height=\"347\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample2.jpg 511w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample2-300x204.jpg 300w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><\/a><figcaption id=\"caption-attachment-2480\" class=\"wp-caption-text\">An If Example, which checks the Exit State of a Command<\/figcaption><\/figure>\n<p>The following example demonstrates that <code>TEST-COMMANDS<\/code> might be any UNIX command that returns an exit status, and that if again returns an exit status of zero:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>exitcheck2.sh<\/em><\/span><\/p>\n<pre class=\"brush:xml\">echo \"Checking...\"\r\nif ! grep Hardy Test1.txt\r\nthen\r\necho 'Hardy not found in File Test1.txt!'\r\nfi\r\necho\r\necho \"...done.\"\r\n<\/pre>\n<figure id=\"attachment_2481\" aria-describedby=\"caption-attachment-2481\" style=\"width: 511px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample3.jpg\"><img decoding=\"async\" class=\"size-full wp-image-2481\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample3.jpg\" alt=\"Another If Example, which checks the Exit State of a Command\" width=\"511\" height=\"347\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample3.jpg 511w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample3-300x204.jpg 300w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><\/a><figcaption id=\"caption-attachment-2481\" class=\"wp-caption-text\">Another If Example, which checks the Exit State of a Command<\/figcaption><\/figure>\n<p>The same result can be obtained as follows:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>exitcheck3.sh<\/em><\/span><\/p>\n<pre class=\"brush:xml\">echo \"Checking...\"\r\ngrep Hardy Test1.txt\r\nif [ $? -ne 0 ]\r\nthen\r\necho 'Hardy not found in File Test1.txt!'\r\nfi\r\necho\r\necho \"...done.\"\r\n<\/pre>\n<figure id=\"attachment_2482\" aria-describedby=\"caption-attachment-2482\" style=\"width: 511px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample4.jpg\"><img decoding=\"async\" class=\"size-full wp-image-2482\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample4.jpg\" alt=\"Another If Example, which checks the Exit State of a Command\" width=\"511\" height=\"347\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample4.jpg 511w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample4-300x204.jpg 300w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><\/a><figcaption id=\"caption-attachment-2482\" class=\"wp-caption-text\">Another If Example, which checks the Exit State of a Command<\/figcaption><\/figure>\n<h3><a name=\"then_numeric\"><\/a>2.3 Numeric comparisons<\/h3>\n<p>The examples below use numerical comparisons:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>numcomp.sh<\/em><\/span><\/p>\n<pre class=\"brush:xml\">echo \"Calculating...\"\r\nnum=`ls | wc -l`\r\necho $num\r\necho\r\n\r\necho \"Checking...\"\r\nif [ \"$num\" -lt \"100\" ]\r\nthen\r\necho \"There exists less then 100 files!\"\r\nfi\r\necho\r\necho \"...done.\"\r\n<\/pre>\n<figure id=\"attachment_2483\" aria-describedby=\"caption-attachment-2483\" style=\"width: 511px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample5.jpg\"><img decoding=\"async\" class=\"size-full wp-image-2483\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample5.jpg\" alt=\"Another If Example, which makes a Numeric Comparison\" width=\"511\" height=\"347\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample5.jpg 511w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample5-300x204.jpg 300w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><\/a><figcaption id=\"caption-attachment-2483\" class=\"wp-caption-text\">Another If Example, which makes a Numeric Comparison<\/figcaption><\/figure>\n<h3><a name=\"then_string\"><\/a>2.4 String comparisons<\/h3>\n<p>An example of comparing strings for testing the user ID:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>strcomp.sh<\/em><\/span><\/p>\n<pre class=\"brush:xml\">echo \"Checking...\"\r\nif [ \"$(whoami)\" != 'root' ]\r\nthen\r\necho \"You are not logged in as user root.\"\r\nfi\r\necho\r\necho \"...done.\"\r\n<\/pre>\n<figure id=\"attachment_2484\" aria-describedby=\"caption-attachment-2484\" style=\"width: 511px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample6.jpg\"><img decoding=\"async\" class=\"size-full wp-image-2484\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample6.jpg\" alt=\"Another If Example, which makes a String Comparison\" width=\"511\" height=\"347\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample6.jpg 511w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2017\/01\/IfExample6-300x204.jpg 300w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><\/a><figcaption id=\"caption-attachment-2484\" class=\"wp-caption-text\">Another If Example, which makes a String Comparison<\/figcaption><\/figure>\n<h2><a name=\"summary\"><\/a>3. Summary<\/h2>\n<p>In this chapter we learned how to build conditions into our scripts so that different actions can be undertaken. Upon success or failure of a command. The actions can be determined using the if statement. This allows you to perform arithmetic and string comparisons, and testing of exit code, input and files needed by the script.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is an If &#8211; Condition Example. Sometimes you need to specify different courses of action to be taken in a shell script, depending on the success or failure of a command. The if construction allows you to specify such conditions. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ulp id=&#8217;SG9vnnKgRCD7iZxf&#8217;] &nbsp; The most compact &hellip;<\/p>\n","protected":false},"author":35,"featured_media":185,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[108,109],"class_list":["post-2468","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash","tag-bash","tag-if"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Bash If - Statement Example - System Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"This is an If Example. Sometimes you need to specify different courses of action to be taken in a shell script, depending on variables or exit status.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bash If - Statement Example - System Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"This is an If Example. Sometimes you need to specify different courses of action to be taken in a shell script, depending on variables or exit status.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/\" \/>\n<meta property=\"og:site_name\" content=\"System Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/systemcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2017-01-19T15:15:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-10T10:26:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/bash-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Andreas Pomarolli\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@systemcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@systemcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andreas Pomarolli\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/\"},\"author\":{\"name\":\"Andreas Pomarolli\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/add1d14bc9f6218cde89909a5d3cc574\"},\"headline\":\"Bash If &#8211; Statement Example\",\"datePublished\":\"2017-01-19T15:15:13+00:00\",\"dateModified\":\"2018-01-10T10:26:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/\"},\"wordCount\":1140,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/bash-logo.jpg\",\"keywords\":[\"bash\",\"if\"],\"articleSection\":[\"BASH\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/\",\"url\":\"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/\",\"name\":\"Bash If - Statement Example - System Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/bash-logo.jpg\",\"datePublished\":\"2017-01-19T15:15:13+00:00\",\"dateModified\":\"2018-01-10T10:26:27+00:00\",\"description\":\"This is an If Example. Sometimes you need to specify different courses of action to be taken in a shell script, depending on variables or exit status.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/#primaryimage\",\"url\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/bash-logo.jpg\",\"contentUrl\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/bash-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.systemcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Shell Scripting\",\"item\":\"https:\/\/www.systemcodegeeks.com\/category\/shell-scripting\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"BASH\",\"item\":\"https:\/\/www.systemcodegeeks.com\/category\/shell-scripting\/bash\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Bash If &#8211; Statement Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#website\",\"url\":\"https:\/\/www.systemcodegeeks.com\/\",\"name\":\"System Code Geeks\",\"description\":\"Operating System Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.systemcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.systemcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/systemcodegeeks\",\"https:\/\/x.com\/systemcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/add1d14bc9f6218cde89909a5d3cc574\",\"name\":\"Andreas Pomarolli\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e36c8f6d54df9db449aa16b2d6ddc1077d50d5e1ff3be0f0b6809081138f01fd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e36c8f6d54df9db449aa16b2d6ddc1077d50d5e1ff3be0f0b6809081138f01fd?s=96&d=mm&r=g\",\"caption\":\"Andreas Pomarolli\"},\"description\":\"Andreas has graduated from Computer Science and Bioinformatics at the University of Linz. During his studies he has been involved with a large number of research projects ranging from software engineering to data engineering and at least web engineering. His scientific focus includes the areas of software engineering, data engineering, web engineering and project management. He currently works as a software engineer in the IT sector where she is mainly involved with projects based on Java, Databases and Web Technologies.\",\"sameAs\":[\"https:\/\/www.systemcodegeeks.com\"],\"url\":\"https:\/\/www.systemcodegeeks.com\/author\/andreas-pomarolli\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Bash If - Statement Example - System Code Geeks - 2026","description":"This is an If Example. Sometimes you need to specify different courses of action to be taken in a shell script, depending on variables or exit status.","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:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/","og_locale":"en_US","og_type":"article","og_title":"Bash If - Statement Example - System Code Geeks - 2026","og_description":"This is an If Example. Sometimes you need to specify different courses of action to be taken in a shell script, depending on variables or exit status.","og_url":"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/","og_site_name":"System Code Geeks","article_publisher":"https:\/\/www.facebook.com\/systemcodegeeks","article_published_time":"2017-01-19T15:15:13+00:00","article_modified_time":"2018-01-10T10:26:27+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/bash-logo.jpg","type":"image\/jpeg"}],"author":"Andreas Pomarolli","twitter_card":"summary_large_image","twitter_creator":"@systemcodegeeks","twitter_site":"@systemcodegeeks","twitter_misc":{"Written by":"Andreas Pomarolli","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/#article","isPartOf":{"@id":"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/"},"author":{"name":"Andreas Pomarolli","@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/add1d14bc9f6218cde89909a5d3cc574"},"headline":"Bash If &#8211; Statement Example","datePublished":"2017-01-19T15:15:13+00:00","dateModified":"2018-01-10T10:26:27+00:00","mainEntityOfPage":{"@id":"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/"},"wordCount":1140,"commentCount":1,"publisher":{"@id":"https:\/\/www.systemcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/bash-logo.jpg","keywords":["bash","if"],"articleSection":["BASH"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/","url":"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/","name":"Bash If - Statement Example - System Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.systemcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/#primaryimage"},"image":{"@id":"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/bash-logo.jpg","datePublished":"2017-01-19T15:15:13+00:00","dateModified":"2018-01-10T10:26:27+00:00","description":"This is an If Example. Sometimes you need to specify different courses of action to be taken in a shell script, depending on variables or exit status.","breadcrumb":{"@id":"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/#primaryimage","url":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/bash-logo.jpg","contentUrl":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/bash-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.systemcodegeeks.com\/shell-scripting\/bash\/bash-if-statement-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.systemcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Shell Scripting","item":"https:\/\/www.systemcodegeeks.com\/category\/shell-scripting\/"},{"@type":"ListItem","position":3,"name":"BASH","item":"https:\/\/www.systemcodegeeks.com\/category\/shell-scripting\/bash\/"},{"@type":"ListItem","position":4,"name":"Bash If &#8211; Statement Example"}]},{"@type":"WebSite","@id":"https:\/\/www.systemcodegeeks.com\/#website","url":"https:\/\/www.systemcodegeeks.com\/","name":"System Code Geeks","description":"Operating System Developers Resource Center","publisher":{"@id":"https:\/\/www.systemcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.systemcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.systemcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.systemcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/systemcodegeeks","https:\/\/x.com\/systemcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/add1d14bc9f6218cde89909a5d3cc574","name":"Andreas Pomarolli","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e36c8f6d54df9db449aa16b2d6ddc1077d50d5e1ff3be0f0b6809081138f01fd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e36c8f6d54df9db449aa16b2d6ddc1077d50d5e1ff3be0f0b6809081138f01fd?s=96&d=mm&r=g","caption":"Andreas Pomarolli"},"description":"Andreas has graduated from Computer Science and Bioinformatics at the University of Linz. During his studies he has been involved with a large number of research projects ranging from software engineering to data engineering and at least web engineering. His scientific focus includes the areas of software engineering, data engineering, web engineering and project management. He currently works as a software engineer in the IT sector where she is mainly involved with projects based on Java, Databases and Web Technologies.","sameAs":["https:\/\/www.systemcodegeeks.com"],"url":"https:\/\/www.systemcodegeeks.com\/author\/andreas-pomarolli\/"}]}},"_links":{"self":[{"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/posts\/2468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/comments?post=2468"}],"version-history":[{"count":0,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/posts\/2468\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/media\/185"}],"wp:attachment":[{"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/media?parent=2468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/categories?post=2468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/tags?post=2468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}