Skip to main content
20 events
when toggle format what by license comment
Mar 3, 2023 at 20:59 comment added jrodman This is a good efficient solution, and probably works with dash as well as bash, but will fail on bourne. It might be worth mentioning that this expects posix shell pattern expansion, which will be available pretty much anywhere on Linux but may fail on aging commercial unix systems.
Mar 3, 2020 at 11:34 comment added leamas Really nice solution. One possible drawback: it requires that the path $1 exists, at least the directory parts of it.
Oct 20, 2017 at 20:51 comment added Alexander Klimetschek @cxw Thanks, good find. This was only a problem with single-level absolute file paths such as /file.txt, while /dir or /dir/file.txt worked before. I fixed it by incorporating your change.
Oct 20, 2017 at 20:48 history edited Alexander Klimetschek CC BY-SA 3.0
add support for singel-level absolute paths such as /file.txt
Oct 20, 2017 at 15:44 comment added cxw Alexander, thanks very much! I edited to if [[ $1 = /* ]] ; then echo "$1" ; elif [[ $1 = */* ... in the file branch so it could also handle absolute paths as input.
Jul 18, 2017 at 18:39 history edited Alexander Klimetschek CC BY-SA 3.0
improved sample
May 23, 2017 at 12:34 history edited URL Rewriter Bot
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Jan 12, 2016 at 23:47 history edited Alexander Klimetschek CC BY-SA 3.0
clarified this is a shell function
Jul 1, 2015 at 20:27 comment added Bruno Bronosky I was looking for something like readlink which gives the realpath with the symlink expanded. Upon closer examination, this is not the right question for that.
Jul 1, 2015 at 19:28 comment added Alexander Klimetschek @BrunoBronosky it works fine for me with symlinks. Do you have an example that does not work? Note that it will return the absolute path (in case of symlinks based on the symlink you pass in), not the "canonical" path, which I would consider having a separate tool/function for that is likely much more difficult to implement in a simple shell function.
Jul 1, 2015 at 12:39 comment added Bruno Bronosky This is a well thought out answer, but it doesn't work for symbolic links to files, only to directories. So, if you have a script that looks for a config file in the same directory as the script, you can't symlink the script to ~/bin. This is a pretty common practice for tools that are under version control.
Mar 27, 2015 at 1:37 comment added Alexander Klimetschek @Six True, updated. The braces ( ... ) are still needed, so the cd happens in a subshell, as we don't want to change the working directory for any callers of abspath.
Mar 27, 2015 at 1:35 history edited Alexander Klimetschek CC BY-SA 3.0
small improvement: don't call echo where it's not needed
Mar 26, 2015 at 14:07 comment added Six I looked through dozens of half-cocked solutions and this definitely appears to be the most concise and accurate bash-only solution. You could slim it even more by replacing echo "$(cd "$1"; pwd)" with (cd "$1"; pwd). There is no need for echo here.
Dec 27, 2014 at 7:17 history edited Alexander Klimetschek CC BY-SA 3.0
fix to support spaces in directory names (arguments to cd were not properly quoted)
Dec 24, 2014 at 11:52 comment added george This doesn't work for directories with spaces, i.e.: $ abspath 'a b c/file.txt' That's because the argument to cd isn't quoted. To overcome this, instead of quoting the entire argument to echo (is there a reason for these quotes?) quote only the path arguments. I.e. replace echo "$(cd ${1%/*}; pwd)/${1##*/}" with echo $(cd "${1%/*}"; pwd)/${1##*/}.
Apr 11, 2014 at 7:42 history edited Alexander Klimetschek CC BY-SA 3.0
question clarifications
Apr 11, 2014 at 7:36 history edited Alexander Klimetschek CC BY-SA 3.0
proper credits
Apr 11, 2014 at 2:20 history edited Alexander Klimetschek CC BY-SA 3.0
fixed code when relative path is just a filename
Apr 11, 2014 at 2:12 history answered Alexander Klimetschek CC BY-SA 3.0