find scripts directory location

If $0 gives the location of the script you are running, how can I just get the directory where the script is located?

CWD; doesn't work the I want, since if I execute a script located on a network drive from the commandline on my local PC, the working directory would be whatever directory I'm in at my shell.

Who is Participating?

Improve company productivity with a Business Account.Sign Up

GnarOlakConnect With a Mentor Commented:
If what you are asking is how to extract from $0, which has both a path and the name of the script, just the directory then you can use File::Basename thus:

use File::Basename;

$script = basename ($0);
$dir = dirname($0);

print "$script was run from $dir\n";

If that isn't what you need then please elaborate.

Marketing_InsistsAuthor Commented:
Yes, that's it exactly, thanks!
I would probably

my @parts = split('\\\\',$0);


print join "\\",@parts;
oops too late.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.