title: Bash: check if a relative path exists
body: <p>I am writing a shell script that takes in a directory as its only argument.  I need to check if the directory exists before I do anything else.  The directory can be absolute or relative.  I know I can check for the existence of an absolute directory (say, /Users/keith/Documents/TestFolder) using</p>
  
  <pre><code>if [ -d "$1"]; then
      # do something if the absolute directory exists
  fi
  </code></pre>
  
  <p>However, if the working directory is /Users/keith/Documents/ and I call my script and pass just "TestFolder", then the test in this if statement will evaluate to false, even though TestFolder exists within the current working directory.</p>
  
  <p>I have tried converting the directory to an absolute directory using</p>
  
  <pre><code>abs_path=`cd "$1"; pwd`
  </code></pre>
  
  <p>and then testing for the existence of the absolute path using the if statement above. This works fine (recognizes that "TestFolder" exists even if that's all that I pass as the argument) provided that the directory passed to the script does indeed exist, but if it doesn't then this fails.</p>
  
  <p>What I need is a way to determine whether the directory passed as an argument exists, regardless of whether it is passed as an absolute directory or one that is relative to the user's current working directory, and it cannot fail if the directory doesn't exist.</p>
  
  <p>I am clearly no bash expert - any advice will be greatly appreciated!</p>