Mobile and web design development (Android, iPhone, WordPress)
Google+TwitterLinkedinFacebook
Subscribe
HomePHP, MySQL, Server sideA simple way to find out the location of a running shell script

A simple way to find out the location of a running shell script

When a shell script is executed, the default directory would normally be the current directory of the user when the script is called. If you get into a situation wherein you want to determine the location of the shell script when it was called by the user, you can do the following within the shell script:

fullpath=`which $0`
scriptdir=`dirname ${fullpath}`

The scriptdir variable would contain the directory where the running script is stored. The which program returns the full path of the called script (e.g. /usr/bin/mybashscript.sh) while the dirname program returns the directory portion of the argument which was passed to dirname.

One use for it would be if for example you need to know where the shell script was stored because you need to call another script that was also stored in that directory when it was installed.

Leave a Reply

  • Hi, I'm Gerry. Welcome to my blog and sites about mobile, technology, The Pinoy, #crazyidea, games, WordPress, Ubuntu, Mac, iOS, Android, Symbian... I started sites like Ortigas Online and iMakati which focuses on local community information. Read more about Gerry
    Status (Twitter, Linkedin, Facebook...)

    Just found out that the Android-based HTC Sensation XL phone is now in the Philippines for about Php 28,000.

  • Visit my other sites

    • Gerry

      My blog about the Philippines, mobile, technology, games, mac, linux...

    • Apps & Games

      Reviews, news of mobile apps & games for Android, iOS, Blackberry, Symbian...

    • Connected Phone

      The computer is the mobile phone. The always connected phone evolution.

    • Pinoy Me

      Pinoy businesses, enterprises, startups, professionals all over the world

    • Java Midlet

      Java mobile apps & games - news, reviews, tips, mobile phone & devices

  • PINOI Sites

    Are you interested in putting up your own PINOI-enabled local community site? Contact me

    • PINOI

      Enabling local communities on the Internet

    • Makati

      iMakati.com is a site that provides information about Makati City.

    • Ortigas

      Community site that provides information about the Ortigas Center.