title: Bash: `chmod a+x` Not Working When Run Inside Script
body: <p>I'm trying to simulate an update process via script.</p>
  <p>I only care about the update script's exit code (<code>0</code> for success and any other value for failure).</p>
  <p>I have created a simple script called <code>update.sh</code> to simulate an update:</p>
  <pre><code>#!/bin/bash
  
  # 1=true, 0=false
  success=1
  
  if [ $success -eq 1 ] ; then
      # Success.
      exit 0
  else
      # Failure.
      exit 1
  fi
  </code></pre>
  <p>To simulate a downloaded update, I have zipped <code>update.sh</code> into a file called <code>update-file.zip</code>.</p>
  <p>My main script extracts <code>update-file.zip</code> and runs <code>update.sh</code>:</p>
  <pre><code>#!/bin/bash
  
  # Create a fresh update folder.
  rm -rfv /test/update &amp;&amp; mkdir /test/update
  
  # Simulate download by copying zip file to that folder.
  cp -rf /update-file.zip /test/update/
  
  cd /test/update
  unzip -o update-file.zip
  
  # Make the update script executable.
  updateFile=&quot;/test/update/update.sh&quot;
  chmod a+x $updateFile
  
  # Run the update.
  /test/update/update.sh
  
  if [ $? -ne 0 ] ; then
      echo &quot;update failed&quot;
  else
      echo &quot;update success&quot;
  fi
  
  # Delete update folder.
  rm -rfv /test/update
  </code></pre>
  <p>However, when I run my main script (even with <code>sudo</code>), I get the following error message:</p>
  <pre><code>/test/update/update.sh: Permission denied
  </code></pre>
  <p>Not even using a service (that uses <code>root</code> to do everything) helped, as I still got the same error message.</p>
  <p>It seems that <code>chmod a+x</code> is not working when run inside scripts, because if I run <code>chmod a+x /test/update/update.sh</code> on the terminal, everything works fine.</p>
  <p>Whenever <code>chmod a+x</code> is run from a script, I get &quot;Permission denied&quot; errors when trying to run the affected script.</p>
  <p>What puzzles me is that when I run <code>ls -l /test/update/update.sh</code>, I get the following:</p>
  <pre><code>-rwxrwxrwx 1 root root 131 Sep  9  2020 /test/update/update.sh
  </code></pre>
  <p>The output is the same regardless of whether <code>chmod a+x</code> is run from the terminal or a script.</p>
  <p>I am on <code>Ubuntu Server 20.04.1 LTS (Focal Fossa)</code>.</p>