Skip to content


Bash: Get current script name and absolute paths

#!/usr/bin/env bash

echo "           \\$0: $0"

_my_name=`basename $0`
if [ "`echo $0 | cut -c1`" = "/" ]; then
  _my_path=`dirname $0`
else
  _my_path=`pwd`/`echo $0 | sed -e s/$_my_name//`
fi

echo "     Filename: $_my_name"
echo "Absolute path: $_my_path"

Posted in Bash, Linux, Tips and Tricks.


9 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. nanocell says

    you could just use the “basename” utility to get the filename, e..g
    basename /foo/bar/test.txt
    returns:
    test.txt

  2. jtanium says

    That’s what I’m doing to get the file name, here:

    _my_name=`basename $0`

    The real point is to get the absolute path of the directory containing the script.

    If you are familiar with Ruby, you can accomplish the same thing with File.dirname(__FILE__)

  3. iongion says

    Are you sure this works, using this exactly like in your sample of centos 5.3

  4. jtanium says

    Yeah, just to make sure:

    [jtanium@flobot ~]$ cat script_name.sh 
    #!/usr/bin/env bash
    
    echo "           \\$0: $0"
    
    _my_name=`basename $0`
    if [ "`echo $0 | cut -c1`" = "/" ]; then
      _my_path=`dirname $0`
    else
      _my_path=`pwd`/`echo $0 | sed -e s/$_my_name//`
    fi
    
    echo "     Filename: $_my_name"
    echo "Absolute path: $_my_path"
    [jtanium@flobot ~]$ ./script_name.sh 
               \./script_name.sh: ./script_name.sh
         Filename: script_name.sh
    Absolute path: /home/jtanium/./
    [jtanium@flobot ~]$ 
    

    It does make me wonder what the deal is the the first echo $0 thing is…

  5. nicholas tung says

    “$0” is the name of the script file. It’s not by default an absolute path.

    With respect, this is not a clean solution; readlink as in http://fritzthomas.com/open-source/linux/384-how-to-get-the-absolute-path-within-the-running-bash-script/ is much better. The style is also not very good and breaks with spaces (the link above does too; it’s not my example). Try (with outer quotes)
    “$(readlink -f “$0″)”
    “$(dirname “$(readlink -f “$0″)”)”

    Despite what it looks, the inside of $() seems to start some sort of new environment, so the quotes around the “$0” behave like you’d expect them to.

    regards,
    Nicholas

  6. jtanium says

    @nicholas,

    That’s pretty sweet. I’ve never seen that before, and I’ll definitely use it.

    Thanks.

  7. Mindya Bizness says

    in bash you can also use the following to get the path of the script.

    BaseDir=$(dirname $(which ${0}))

  8. jtanium says

    Bear in mind, that is only going to work if the script is in your path. Me, I’m not going to trust that.

  9. ishi says

    Speaking of bash, you could do it like this:

    pushd $(dirnamr $0) > /dev/null
    my_path=$(pwd)
    popd > /dev/null > /dev/null

    You could do much the same with cd (and cd -), but I don’t remember if ‘cd -‘ is any more portable than pushd. Much the same with just remembering the ‘old pwd’.

    It has a very nice feature of removing junk from the file name, like when you call your script with something like

    ../../one/dir/../../..///another/dir/../../../../usr/bin/script.sh

    which might result from strange path concatenation (it *does* happen).

    Before you ‘scream’, yes, I know, this method assumes, that the executor has +x for the directory containing the script. Oh well.. Show me any sane person, who removes x from directories, but permits execution scripts therein 😉



Some HTML is OK

or, reply to this post via trackback.