# Getting path of current script

**URL:** <https://www.ruby-forum.com/t/getting-path-of-current-script/73462>\
**Category:** Ruby\
**Created:** [October 24, 2006, 8:06pm UTC](https://www.ruby-forum.com/t/getting-path-of-current-script/73462 "2006-10-24T20:06:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 24, 2006, 8:06pm UTC](https://www.ruby-forum.com/t/getting-path-of-current-script/73462/1 "2006-10-24T20:06:03Z")

</div>

Is there a simpler way to get the absolute path of the current script?

path = File.dirname(File.expand\_path( **FILE** ))

File.dirname( **FILE** ) by itself can return a relative or absolute path.

Joe

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 24, 2006, 8:31pm UTC](https://www.ruby-forum.com/t/getting-path-of-current-script/73462/2 "2006-10-24T20:31:38Z")

</div>

On Wed, 25 Oct 2006, Joe R. MUDCRAP-CE wrote:

> Is there a simpler way to get the absolute path of the current script?
> 
> path = File.dirname(File.expand\_path( **FILE** ))
> 
> File.dirname( **FILE** ) by itself can return a relative or absolute path.
> 
> Joe

just a note, what you have above is not the dir for the current script,  
it’s  
the dir for the current file - eg the value will be the same whether the  
file  
is run or required/loaded. for the current script you need

path = File.dirname(File.expand\_path($0))

or

File.dirname($0)

-a

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 24, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/getting-path-of-current-script/73462/3 "2006-10-24T20:39:04Z")

</div>

removed\_email\_address@domain.invalid wrote:

> File.dirname($0)  
> I think the main problem is that the second solution is not an abs path  
> if you type “./script” or even “…/…/bin/script”.

Your first suggestion is what I would use personally.

-Nate
