Is there a sh equivalent of __FILE__
, to give me the pathname of the currently executing file? POSIX solutions preferred, bash acceptable, thanks.
views:
95answers:
3
+1
A:
Just a thought:
#!/usr/bin/env bash
# "$0" will expand to the name of the script, as called from the command line
readlink -f $0
The MYYN
2010-07-23 14:23:11
That will be incorrect if the script is called via the $PATH variable.
sharth
2010-07-23 14:26:06
Still (see edit)?
The MYYN
2010-07-23 14:28:33
Note that `readlink` isn't entirely portable. OSX (and presumably BSD in general?) have a completely different version - where `-f` isn't supported. Rather, where `-f` means something different (and not helpful here).
Telemachus
2010-07-23 14:29:37
`echo $(readlink -f $0)` works fine on OSX 10.6, just tested it.
The MYYN
2010-07-23 14:30:41
Why are you using `echo`. Doesn't `readlink -f $0` by itself work?
Dennis Williamson
2010-07-23 14:43:44
@Dennis Williamson: Yes sure, thanks, corrected.
The MYYN
2010-07-23 14:45:49
@The MYYN You must have gnu's `coreutils` installed without the `g` prefixes. Here's what happens when I run that command: `readlink: illegal option -- f` (Yes, I'm on 10.6. I actually have gnu's `readlink` as well, but only under the alias `greadlink`. http://pastie.org/1057557 for an example.)
Telemachus
2010-07-23 20:29:51
A:
For a bash script solution: http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-in
karlphillip
2010-07-23 14:23:40