Skip to content

Instantly share code, notes, and snippets.

@EvanK
Created February 2, 2011 21:21
Show Gist options
  • Save EvanK/808463 to your computer and use it in GitHub Desktop.
Save EvanK/808463 to your computer and use it in GitHub Desktop.
dirname(realpath($SOMEPATH)) in bash
#!/bin/sh
# 1. takes $0 (the currently running script)
# 2. puts it through `readlink -f` to resolve any symlinks
# 3. puts it through `dirname` to remove the filename
working_dir=$(dirname $(readlink -f $0))
# so if i symlink a script from /home/ekaufman/myinit.sh to /etc/init.d/mything then even if I
# execute /etc/init.d/mything, ${working_dir} will be "/home/ekaufman"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment