commandlinefu.com is the place to record those command-line gems that you return to again and again.
You can sign-in using OpenID credentials, or register a traditional username and password.
Subscribe to the feed for:
I think this is the cleanest way of getting the current working directory of a script. It also works on osx, Linux, and probably bsd as well..
There are 3 alternatives - vote for the best!
Invoked from within a shell script, this will print the directory in which the script resides. Doesn't depend on external tools, /proc/self/*, etc.. (`echo` is a shell builtin.) To see the *current working* directory of a script, use `pwd`.
This is an alternative to another command using two xargs. If it's a command you know there's only one of, you can just use:
ls -l /proc/$(pgrep COMMAND)/cwd
I submitted a command like this without $0 if $BASH_SOURCE is unset. Therefor, it did only work when using ./script, not using 'sh script'. This version handles both, and will set $mydir in a script to the current working directory. It also works on linux, osx and probably bsd.
If you can do better, submit your command here.
You must be signed in to comment.