title: bash scripting: how to find the absolute path of "symlink/.."?
body: <p>Given two files:</p>
  
  <pre><code>generic/scripts/hello.sh
  parent/scripts -&gt; generic/scripts
  </code></pre>
  
  <p>Upon calling <code>parent/scripts/hello.sh</code> from any location, I would like (in the script) to find the full path of the parent directory. In this case <code>parent</code>. </p>
  
  <p>The main issue is that <code>parent/scripts/..</code> refers to <code>generic</code> in unix. On the other hand, everything involving regexes is not generic and may be error prone.</p>
  
  <p>Solutions that don't work:</p>
  
  <pre><code>`dirname $0`/..
  realpath  `dirname $0`/..
  readlink -f `dirname $0`/..
  `cd *something*/..; pwd`
  `perl ... abs_path(...)`
  </code></pre>
  
  <p>All these will point to <code>generic</code> and not <code>parent</code> because of the symbolic link. </p>
  
  <p>Everything involving regular expressions are not adaptable/generic, may fail for more complexes paths. There might be other <code>..</code> and symlinks in the path, you want the grand-parent, it's a directory name involving <code>..</code>, you call it via $PATH...</p>
  
  <p>Moreover, I would like it to work in any case, even when it is called via <code>$PATH</code>.</p>
  
  <p>Any simple safe solution for this simple problem? I mean it's just getting the parent directory after all!</p>
  
  <p><em><strong>What I used:</em></strong></p>
  
  <pre><code>dir=$( dirname $( cd `dirname $0` &gt;/dev/null; pwd ) )
  </code></pre>
  
  <p>Dunno if it is perfect but it seems to behave as expected.</p>