Facebook Twitter Instagram
    WiredRevolution.com
    • Home
    • About
    • Contact Us
    • Essential Linux Commands
    • Sitemap
    Facebook Twitter Instagram
    WiredRevolution.com
    bash

    Have a Bash script determine it’s own location

    RyanBy RyanDecember 15, 2010Updated:December 15, 20101 Comment1 Min Read
    Facebook Twitter Pinterest LinkedIn Reddit WhatsApp Email

    At some point it may be helpful to have a BASH script dynamically determine the location of itself when executed from anywhere on the system. The following code will produce the canonicalized absolute pathname of the script, as well as the directory that it resides in.

    The script first determines if the the first argument which is a path to the script is a relative or absolute path. If it is a relative path it will prepend the current working directory. Finally the entire path is canonicalized which resolves all symlinks and removes the extra “./”, “../” and ‘/’ characters.

    Here is the script:

    if [[ "$0" =~ ^/ ]]
    then
    SCRIPT_LOCATION=$(readlink -f "$0")
    else
    SCRIPT_LOCATION=$(readlink -f "$(pwd)/""$0")
    fi
    SCRIPT_DIR=$(dirname $SCRIPT_LOCATION)
    echo $SCRIPT_LOCATION
    echo $SCRIPT_DIR

    Now for a quick test. Lets assume our script is located here:
    /home/ryan/scripts/mylocation.sh

    Executing the script in the same directory:
    $ ./mylocation/home/ryan/scripts/mylocation.sh
    /home/ryan/scripts

    Executing in another directory with the absolute path produces the same results:
    $ /home/ryan//////scripts/../scripts/mylocation.sh/home/ryan/scripts/mylocation.sh
    /home/ryan/scripts

    Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Email
    Previous ArticleRemove the envelope icon from the Gnome indicator applet
    Next Article Build the perfect HTPC with Boxee and the Dell Zino HD

    Related Posts

    How to test the syntax of a Python script

    Safely and atomically change a symlink target

    Sync your iPod Shuffle with Linux

    Most Commented
    March 12, 2009

    Fix blue tinted video in Ubuntu

    September 10, 2010

    Setup SSH access between VirtualBox Host and Guest VMs

    March 8, 2011

    Install GNOME Shell in Ubuntu 10.10 Maverick

    April 4, 2009

    Setup the PS3 Bluetooth Controller on Ubuntu

    October 22, 2008

    How to correctly use LD_LIBRARY_PATH

    Recent Comments
    • Execute command on linux virtual machine (or server) from windows commandline on Setup SSH access between VirtualBox Host and Guest VMs
    • Solved: How to SSH to a VirtualBox guest externally through a host? - Daily Developer Blog on Setup SSH access between VirtualBox Host and Guest VMs
    • How to SSH to a VirtualBox guest externally through a host? [closed] – Code D3 on Setup SSH access between VirtualBox Host and Guest VMs
    • How to copy and paste from VirtualBox? [duplicate] on Setup SSH access between VirtualBox Host and Guest VMs
    • Jackie Laguna on Fix OpenGL: ChoosePixelFormat SketchUp error in WINE
    © 2025 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.