Referring to the directory that a script is running from?

Discussion in 'Mac Programming' started by AquaMethod, Jan 21, 2009.

  1. macrumors member

    Joined:
    Jun 17, 2008
    #1
    I'd like to create a bash script that would live on a USB thumb drive and copy over some files from that same USB drive, onto the Mac it's plugged into. How can I refer to the path the script is executing from, even if the name of the drive has changed or it's not mounted under /Volumes ?

    I suppose it's possible to mess around with the $0 parameter, but will that work even if the script is created as a .command file, and run by the Finder?
     
  2. Moderator emeritus

    mkrishnan

    Joined:
    Jan 9, 2004
    Location:
    Grand Rapids, MI, USA
  3. thread starter macrumors member

    Joined:
    Jun 17, 2008
    #3
    Ah, that makes sense. (Not sure why I didn't think to search SO first...thanks for doing so)

    So the cleanest solution would probably be

    Code:
    scriptPath=`dirname $0`
    And then use $scriptPath from there one? Seems like that'll work.
     
  4. Moderator emeritus

    mkrishnan

    Joined:
    Jan 9, 2004
    Location:
    Grand Rapids, MI, USA
    #4
    Yeah, something like that should work just fine. :)
     

Share This Page