title: Cd inside a Bash FUNCTION
body: <p>My first question on SO, so apologies if not doing something right! </p>
  
  <p>There are many questions on the internet about using cd inside a script, but my problem is in using cd inside a bash function I put in my <code>.bashrc</code>. Its task is to find a file and go to the working directory of the file. In case of multiple files found, I just goes to the first one. Here it is:</p>
  
  <pre><code>fcd() {
  cd $PWD
  if [ -z "$1" ]; then
      echo 'Specify a file name to find'
  else
      found_dir=$( find . -name $1 -type f -printf \"%h/\" -quit )
      echo $found_dir
      if [ -z "$found_dir" ]; then
          echo "No file found. Directory was not changed"
      else
          cd $found_dir
      fi
  fi
  }
  </code></pre>
  
  <p>However, when I use it, the directory is found, but trying to <code>cd $found_dir</code> results in the message:</p>
  
  <pre><code>cd: (directory_here): No such file or directory
  </code></pre>
  
  <p>I've excluded possibility of the path being wrong - by copying the output of <code>echo $found_dir</code> and pasting it in front of a <code>cd</code> the directory is changed succesfully. Any ideas?</p>
  
  <p>Thanks,</p>
  
  <p>Jakub</p>