title: How to assign a variable inside if condition in shell script
body: <p>I would like to know how to assign a variable inside if block in shell script..</p>
  
  <p>Below is my code..</p>
  
  <pre><code>if [[ -z "$MMBOX_PATH" || -z "$BACKUP_PATH" || -z "$REMOTE_SERVER" || -z "$LOG_PATH" ]]
  then
      echo -e "Must Provide All Required Paths [$FLAG is Empty].."
      exit 1
  fi
  </code></pre>
  
  <p>The above code will run whenever it found empty variable, but I also wants to know which variable is empty (E.g., In above code suppose if LOG_PATH variable is empty then it should display in echo output in place of $FLAG )</p>
  
  <p>I tried following codes..</p>
  
  <pre><code>if [[ `FLAG='MMBOX_PATH'` &amp;&amp; -z "$MMBOX_PATH" || `FLAG='BACKUP_PATH'` &amp;&amp; -z "$BACKUP_PATH" || `FLAG='REMOTE_SERVER'` &amp;&amp; -z "$REMOTE_SERVER" || `FLAG='LOG_PATH'` &amp;&amp; -z "$LOG_PATH" ]]
  then
      echo -e "Must Provide All Required Paths [$FLAG is Empty].."
      exit 1
  fi
  </code></pre>
  
  <p>But above code returns false hence it is not printing the content inside echo.</p>
  
  <p>I also tried to keep FLAG variable before condition execution, but every time it returns 'Nothing'</p>
  
  <pre><code>if FLAG='MMBOX_PATH' &amp;&amp; [[ -z "$MMBOX_PATH" ]]
  then
      echo -e "Must Provide All Required Paths [$FLAG is Empty].."
      exit 1
  fi
  </code></pre>
  
  <p>In above case I'm getting FLAG='MMBOX_PATH' in output but if I add one more condition to that if nothing is printing (Means if I check same thing for BACKUP_PATH,REMOTE_SERVER..)</p>
  
  <pre><code>if FLAG='MMBOX_PATH' &amp;&amp; [[ -z "$MMBOX_PATH" ]] &amp;&amp; FLAG='LOG_PATH' &amp;&amp; [[ -z "$LOG_PATH" ]]
  then
      echo -e "Must Provide All Required Paths [$FLAG is Empty].."
      exit 1
  fi
  </code></pre>
  
  <p>In this case nothing is printing even though MMBOX_PATH present and LOG_PATH empty. </p>
  
  <p><strong>Note: Using if condition each and every variable it is possible to know which variable is empty,but I don't want to extend my lines with if-else conditions</strong> I just want to know in that if block itself how to assign a variable and prints once condition is true.</p>
  
  <p>Can anybody help me how to get empty variable..? (/bin/bash)</p>