get parent directory of a file in bash
<p>How do I get a parent directory for a file?</p>

<p>I want it to be safe on all kind of names:</p>

<pre><code>.
..
path/to/my/file
/absolute/path/to/my/file
'-rf --no-preserve-root whatever'/test.zip
(symbolic links)
`'"`'{(})
</code></pre>

<p>I am more interested getting the canonical location on the file system than in traversing the path stated in the filename.</p>

<p>Note that there are similar questions to this one, but none of them focuses on correctness, relative/absolute paths and "unsafe" names:</p>

<p>[1] <a href="https://stackoverflow.com/questions/8426058/bash-get-the-parent-directory-of-current-directory">bash get the parent directory of current directory</a></p>

<p>[2] <a href="https://stackoverflow.com/questions/20196034/retrieve-parent-directory-of-script">Retrieve parent directory of script</a></p>

<p>[3] <a href="https://stackoverflow.com/questions/6509650/bash-filepath-to-parent-directory-of-file">bash filepath to parent directory of file</a></p>