Difference between revisions of "Bash if elif else fi"

From thelinuxwiki
Jump to: navigation, search
Line 1: Line 1:
 
Bash If..elif..else..fi
 
Bash If..elif..else..fi
 
+
<source lang="bash">
 
   if [ conditional expression1 ]
 
   if [ conditional expression1 ]
 
   then
 
   then
Line 16: Line 16:
 
     statement5
 
     statement5
 
   fi
 
   fi
 
+
</source>
 
== example ==
 
== example ==
 
<source lang="bash">
 
<source lang="bash">
Line 24: Line 24:
 
</source>
 
</source>
 
OR example
 
OR example
 +
<source lang="bash">
 
  <nowiki>
 
  <nowiki>
 
  if [[ "$VARIABLE" == "something" || "$VARIABLE" == "else" ]]; then</nowiki>
 
  if [[ "$VARIABLE" == "something" || "$VARIABLE" == "else" ]]; then</nowiki>
 
     some_command
 
     some_command
 
  fi
 
  fi
 
+
</source>
 
conditional based on string compare
 
conditional based on string compare
  

Revision as of 13:17, 16 April 2021

Bash If..elif..else..fi

  if [ conditional expression1 ]
  then
    statement1
    statement2
    .
  elif [ conditional expression2 ]
  then
    statement3
    statement4
    .
    .
    .
  else
    statement5
  fi

example

  if [ "$opt" = "some string" ]; then
    some_command
  fi

OR example

 <nowiki>
 if [[ "$VARIABLE" == "something" || "$VARIABLE" == "else" ]]; then</nowiki>
    some_command
 fi

conditional based on string compare

if a variable contains a string...

 string='foobar'
 if [[ $string == *"bar"* ]]; then
   echo "your bar is in my foo!"
 fi