title: Why does this shell script exit without reading `case`?
body: <p>I am writing a <code>bash</code> shell script for practice, but can't seem to fix its behavior. The script copies an array of files and directories into a given backup directory, <em>$path</em>, and compresses it to a <em>.tar.gz</em> file. </p>
  
  <p>The iteration I'm working on should check if said <em>.tar.gz</em> file already exists using the naming pattern for backup directory (<em>$date_value.tar.gz</em>), and has specific cases depending on the answer. The script is expected to be ran from main project directory for now.</p>
  
  <p>The following code is the part that goes wrong.</p>
  
  <pre><code>if [ -f "$path/$date_value.tar.gz" ] ; then
  
    echo "A $date_value.tar.gz directory already exists."
  
    read -p "Do you wish to update directory ? (Y/N)"
  
    echo # Moves to new line; for user experience purpose only
  
    case $REPLY in
  
          # Case when user wrote "Y" or "y" as in Yes
          " [[ $REPLY =~ ^[Yy]$ ]] " )
  
              # Extract archive
              tar xf "$path/$date_value.tar.gz"
  
              # Synchronise directories
              rsync --update -raz --progress \ 
                    --include="$current_path${files_array[@]}" "$path/$date_value" \
                    --exclude="*"
  
     exit 0;
  ;;
  
          # Case when user wrote anything else
          " [[ $REPLY =~ ^[*]$ ]] " )
  
              echo "Script didn't make any change and stopped itself."
  
      exit 1;
  esac
  
  # Else if backup directory doesn't exist yet
  else
  
      # Make a directory using the date
      mkdir "$path/$date_value"
  
      # Loops over the whole array and copies files/directories
      # recursively to given directory with current rights
      for i in ${files_array[@]}; do
              cp -ar ${i} /home/robin/backup/by_date/"$date_value"/
      done
  
      # Goes to backup directory
      cd "$path"
  
      # Compress backup directory into tarball AND(=&amp;&amp;) removes it if successfull
      tar cfM "$date_value.tar.gz" "$date_value" &amp;&amp; rm -Rf "$date_value"
  
  fi
  </code></pre>
  
  <p>When run in <code>bash -x</code> mode, I see that it proceeds as expected until the <code>case $REPLY in</code> line, then suddenly stops without running through the case. I put an <code>exit 1</code> as a test after the last closing <code>fi</code> and can confirm the script simply jumps past <code>case</code> and stops, since there are no further instructions. </p>
  
  <p><strong>What is going wrong in there, and why is it ditching the <code>case</code>?</strong></p>
  
  <p>Documentation used to write this code:</p>
  
  <ul>
  <li>Flow control : <a href="http://linuxcommand.org/wss0120.php" rel="nofollow noreferrer">http://linuxcommand.org/wss0120.php</a></li>
  <li><a href="https://stackoverflow.com/questions/59838/check-if-a-directory-exists-in-a-shell-script">Check if a directory exists in a shell script</a></li>
  <li>Test building : <a href="http://tldp.org/LDP/abs/html/testconstructs.html#DBLBRACKETS" rel="nofollow noreferrer">http://tldp.org/LDP/abs/html/testconstructs.html#DBLBRACKETS</a></li>
  <li>Test building using [[ ]] specific operators : <a href="https://stackoverflow.com/questions/19441521/bash-regex-operator">Bash regex =~ operator</a></li>
  </ul>
  
  <p><strong>Run on <em>Debian</em>, with <code>bash</code> as terminal.</strong></p>