title: Self updating bash script from github
body: <p>I am trying to make my script to check if there is an update from my repo in github and then get the updates and replace old code with new one and run the new code “not the old one”. I came up with this but it updates after it finishes </p>
  
  <pre><code>self_update() {
      cd $(dirname $0)
      git fetch &gt; a.txt 1&gt; /dev/null 2&gt; /dev/null
      git reset --hard &gt;&gt; a.txt 1&gt; /dev/null 2&gt; /dev/null
      git pull &gt;&gt; a.txt 1&gt; /dev/null 2&gt; /dev/null
      rm a.txt
      chmod +x "$(basename $0)"
      cd -
  }
  self_update
  echo “some code”
  </code></pre>
  
  <p>Edit: I found the below code here <a href="https://stackoverflow.com/questions/35365799/shell-script-self-update-using-git/35365800">stackoverflow</a> and it updates my script. However, it goes into a loop and never runs the new or old code, not sure why.</p>
  
  <pre><code>#!/bin/bash
  
  SCRIPT=$(readlink -f "$0")
  SCRIPTPATH=$(dirname "$SCRIPT")
  SCRIPTNAME="$0"
  ARGS="( $@ )"
  BRANCH="master"
  
  self_update() {
      cd $SCRIPTPATH
      git fetch
  
      [ -n $(git diff --name-only origin/$BRANCH | grep $SCRIPTNAME) ] &amp;&amp; {
          echo "Found a new version of me, updating myself..."
          git pull --force
          git checkout $BRANCH
          git pull --force
          echo "Running the new version..."
          exec "$SCRIPTNAME" "${ARGS[@]}"
  
          # Now exit this old instance
          exit 1
      }
      echo "Already the latest version."
  }
  self_update
  echo “some code”
  
  </code></pre>
  
  <p>Repeated Output:</p>
  
  <pre><code> Found a new version of me, updating myself...
   HEAD is now at 5dd5111 Update tool
   Already up to date
   Already on ‘master’
   Your branch is up to date with origin/master
  
  </code></pre>
  
  <p>It does not stop printing the output till i CTRL-C
  Output: Executed with: bash -x /opt/script/firstScript -h </p>
  
  <pre><code>++ readlink -f /opt/script/firstScript
  + SCRIPT=/opt/script/firstScript  
  ++ dirname /opt/script/firstScript
  + SCRIPTPATH=/opt/script                                
  + SCRIPTNAME=/opt/script/firstScript
  + ARGS='( -h )'
  + BRANCH=master
  + self_update      
  + cd /opt/script
  + git fetch                                                
  ++ git diff --name-only origin/master
  ++ grep /opt/script/firstScript
  + '[' -n ']'                                               
  + echo 'Found a new version of me, updating myself...'
  Found a new version of me, updating myself...
  + git pull --force 
  Already up to date.  
  + git checkout master
  Already on 'master'
  Your branch is up to date with 'origin/master'.
  + git pull --force
  Already up to date.
  + echo 'Running the new version...'
  Running the new version...
  + exec bash -x /opt/script/firstScript '( -h )'
  ++ readlink -f /opt/script/firstScript
  + SCRIPT=/opt/script/firstScript
  ++ dirname /opt/script/firstScript
  + SCRIPTPATH=/opt/script
  + SCRIPTNAME=/opt/script/firstScript
  + ARGS='( ( -h ) )'
  + BRANCH=master
  + self_update
  + cd /opt/script
  + git fetch
  ++ git diff --name-only origin/master
  ++ grep /opt/script/firstScript
  + '[' -n ']'
  + echo 'Found a new version of me, updating myself...'
  Found a new version of me, updating myself...
  + git pull --force
  Already up to date.
  + git checkout master
  Already on 'master'
  Your branch is up to date with 'origin/master'.
  + git pull --force
  Already up to date.
  + echo 'Running the new version...'
  Running the new version...
  + exec bash -x /opt/script/firstScript '( ( -h ) )'
  ++ readlink -f /opt/script/firstScript
  + SCRIPT=/opt/script/firstScript
  ++ dirname /opt/script/firstScript
  + SCRIPTPATH=/opt/script
  + SCRIPTNAME=/opt/script/firstScript
  + ARGS='( ( ( -h ) ) )'
  + BRANCH=master
  + self_update
  + cd /opt/script
  + git fetch
  ++ git diff --name-only origin/master
  ++ grep /opt/script/firstScript
  + '[' -n ']'
  + echo 'Found a new version of me, updating myself...'
  Found a new version of me, updating myself...
  + git pull --force
  Already up to date.
  + git checkout master
  Already on 'master'
  Your branch is up to date with 'origin/master'.
  + git pull --force
  ^C
  
  </code></pre>
  
  <p>Output: Executed with: bash /opt/script/firstScript -h </p>
  
  <pre><code>Found a new version of me, updating myself...
  Already up to date.
  Your branch is up to date with 'origin/master'.
  Already up to date.
  Running the new version...
  Found a new version of me, updating myself...
  Already up to date.
  Your branch is up to date with 'origin/master'.
  Already up to date.
  Running the new version...
  Found a new version of me, updating myself...
  Already up to date.
  Your branch is up to date with 'origin/master'.
  Already up to date.
  Running the new version...
  Found a new version of me, updating myself...
  Already up to date.
  Your branch is up to date with 'origin/master'.
  Already up to date.
  Running the new version...
  Found a new version of me, updating myself...
  Already up to date.
  Your branch is up to date with 'origin/master'.
  Already up to date.
  Running the new version...
  Found a new version of me, updating myself...
  Already up to date.
  Your branch is up to date with 'origin/master'.
  Already up to date.
  Running the new version...
  Found a new version of me, updating myself...
  Already up to date.
  Your branch is up to date with 'origin/master'.
  Already up to date.
  Running the new version...
  Found a new version of me, updating myself...
  ^C
  </code></pre>