Skip to content

Instantly share code, notes, and snippets.

@nicerobot
Created May 14, 2012 02:53
Show Gist options
  • Save nicerobot/2691519 to your computer and use it in GitHub Desktop.
Save nicerobot/2691519 to your computer and use it in GitHub Desktop.
Canonicalize a file's path i.e. a cross platform readlink
#!/bin/sh
# eg. dir=$(canonical $0)
canonical() {
local d="$(\dirname ${1})"
local f="$(\basename ${1})"
(
\cd ${d} >/dev/null 2>&1
while [ -h "${f}" ] ; do
\cd $(\dirname $(\readlink ${f})) >/dev/null 2>&1
done
\pwd -P
)
}
@ORESoftware
Copy link

ORESoftware commented Nov 25, 2016

just curious what's the diff between

canonical(){

}

and

function canonical {

}

also curious why you are using

${1}

instead of

${0}

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment