# Ruby Scripts Location

**URL:** <https://www.ruby-forum.com/t/ruby-scripts-location/97801>\
**Category:** Ruby\
**Created:** [July 10, 2006, 5:54pm UTC](https://www.ruby-forum.com/t/ruby-scripts-location/97801 "2006-07-10T17:54:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [July 10, 2006, 5:54pm UTC](https://www.ruby-forum.com/t/ruby-scripts-location/97801/1 "2006-07-10T17:54:26Z")

</div>

Hi All

I know this should be very simple but I can’t for the life of me find  
the answer using Pickaxe or Google.

I want to be able to access the location/path of the currently  
executing Ruby script. I thought it was a global variable “$” but  
looking at the lists I’ve seen I can’t find it.

Your help would be much appreciated.

Thanks.

Doug

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [July 10, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/ruby-scripts-location/97801/2 "2006-07-10T17:57:49Z")

</div>

Doug B. wrote:

> I want to be able to access the location/path of the currently  
> executing Ruby script. I thought it was a global variable “$” but  
> looking at the lists I’ve seen I can’t find it.

$0 is probably what you want. Use File.expand\_path on it to get the full  
path.

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [July 10, 2006, 6:00pm UTC](https://www.ruby-forum.com/t/ruby-scripts-location/97801/3 "2006-07-10T18:00:59Z")

</div>

$0, or **FILE** in the main script file.

Gennady.

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [July 11, 2006, 3:27pm UTC](https://www.ruby-forum.com/t/ruby-scripts-location/97801/4 "2006-07-11T15:27:57Z")

</div>

On Jul 10, 2006, at 10:57 AM, Joel VanderWerf wrote:

> $0 is probably what you want. Use File.expand\_path on it to get the  
> full path.

A couple of points to mention. 1) You can use $PROGRAM\_NAME instead  
of $0, it’s  
a little more readable. 2) Make sure you have not done a Dir.chdir  
before the File.expand\_path

% cat path.rb  
puts $PROGRAM\_NAME  
puts File.expand\_path($PROGRAM\_NAME)  
Dir.chdir “pdf”  
puts File.expand\_path($PROGRAM\_NAME)

% ruby path.rb  
path.rb  
/Users/jdf/projects/devel/ruby\_test\_code/path.rb  
/Users/jdf/projects/devel/ruby\_test\_code/pdf/path.rb

> –  
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Jim F.
