Advertisement: Linux VPS from $4/month - contact support for custom offer.
+ Post New Thread
Results 1 to 2 of 2

Thread: Show curent directory/path from which bash script is executed

  1. #1
    Administrator
    Join Date
    Mar 2013
    Posts
    2,654

    Show curent directory/path from which bash script is executed

    How to print/show in variable path to the curent bash script?

    # cat /root/script.sh
    thisscriptdir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
    echo $thisscriptdir
    # sh /root/script.sh
    /root

  2. #2


    Is this useful / helpfull? Yes | No
    #!/bin/bash

    DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
    is a useful one-liner which will give you the full directory name of the script no matter where it is being called from.

    It will work as long as the last component of the path used to find the script is not a symlink (directory links are OK). If you also want to resolve any links to the script itself, you need a multi-line solution:

    #!/bin/bash

    SOURCE="${BASH_SOURCE[0]}"
    while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
    DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
    done
    DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
    This last one will work with any combination of aliases, source, bash -c, symlinks, etc.

    Beware: if you cd to a different directory before running this snippet, the result may be incorrect!

    Also, watch out for $CDPATH gotchas, and stderr output side effects if the user has smartly overridden cd to redirect output to stderr instead (including escape sequences, such as when calling update_terminal_cwd >&2 on Mac). Adding >/dev/null 2>&1 at the end of your cd command will take care of both possibilities.

    To understand how it works, try running this more verbose form:

    #!/bin/bash

    SOURCE="${BASH_SOURCE[0]}"
    while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
    TARGET="$(readlink "$SOURCE")"
    if [[ $TARGET == /* ]]; then
    echo "SOURCE '$SOURCE' is an absolute symlink to '$TARGET'"
    SOURCE="$TARGET"
    else
    DIR="$( dirname "$SOURCE" )"
    echo "SOURCE '$SOURCE' is a relative symlink to '$TARGET' (relative to '$DIR')"
    SOURCE="$DIR/$TARGET" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
    fi
    done
    echo "SOURCE is '$SOURCE'"
    RDIR="$( dirname "$SOURCE" )"
    DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
    if [ "$DIR" != "$RDIR" ]; then
    echo "DIR '$RDIR' resolves to '$DIR'"
    fi
    echo "DIR is '$DIR'"
    And it will print something like:

    SOURCE './scriptdir.sh' is a relative symlink to 'sym2/scriptdir.sh' (relative to '.')
    SOURCE is './sym2/scriptdir.sh'
    DIR './sym2' resolves to '/home/ubuntu/dotfiles/fo fo/real/real1/real2'
    DIR is '/home/ubuntu/dotfiles/fo fo/real/real1/real2'

+ Post New Thread

Similar Threads

  1. Replies: 0
    Last Post: 05-31-2015, 03:16 PM
  2. Replies: 0
    Last Post: 02-23-2015, 11:07 AM
  3. Replies: 0
    Last Post: 11-22-2014, 04:42 PM
  4. Replies: 0
    Last Post: 11-15-2014, 02:36 PM
  5. WTS Linux bash script automation service
    By Fli in forum WTS/WTB Programming services
    Replies: 0
    Last Post: 11-08-2013, 12:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
 Protected by : ZB BLOCK  &  StopForumSpam