Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: How do you find the Directory of the open Shell Script File?

  1. #1
    Join Date
    Oct 2010
    Location
    The Big, Bad, World!
    Beans
    120
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Thumbs down How do you find the Directory of the open Shell Script File?

    Say if I have a Shell Script, and it sets up a program that can be downloaded, so the file could be anywhere. How to I (in Shell, or some other means), get the directory of the Current Shell Script which is open (the one that sets up the program)?

    Cheers for any help....
    x-D
    Choose Freedom, Choose Linux, Choose Ubuntu.

  2. #2
    Join Date
    Jun 2011
    Beans
    57
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: How do you find the Directory of the open Shell Script File?

    Do you want:
    Code:
    pwd
    Print working directory

  3. #3
    Join Date
    Jun 2008
    Location
    UK
    Beans
    282
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: How do you find the Directory of the open Shell Script File?

    Quote Originally Posted by x-D View Post
    Say if I have a Shell Script, and it sets up a program that can be downloaded, so the file could be anywhere. How to I (in Shell, or some other means), get the directory of the Current Shell Script which is open (the one that sets up the program)?
    You have the $PWD environment variable and also the builtin pwd command and /bin/pwd. Take your pick. If you want the working directory in which the shell script is invoked because you intend to change the directory try
    PHP Code:
    startDir=$PWD 
    somewhere near the start of your script (ie before the first cd command).

    Andrew

  4. #4
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: How do you find the Directory of the open Shell Script File?

    $PWD gives you the current directory, which isn't the directory where the shell script file is (can be invoked with an explicit path, or be somewhere in the PATH). The directory where the script comes from is obtained by:
    Code:
    mydir=$(dirname $0)
    In other words:
    Code:
    #! /bin/bash
    
    me=$0
    mydir=$(dirname $0)
    
    echo "Me: $me"
    echo "My dir: $mydir"

  5. #5
    Join Date
    Oct 2010
    Location
    The Big, Bad, World!
    Beans
    120
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Thumbs down Re: How do you find the Directory of the open Shell Script File?

    So if I wanted to use this information to copy the folder the shell script was in, to another folder how would I go about doing so?
    Choose Freedom, Choose Linux, Choose Ubuntu.

  6. #6
    Join Date
    Mar 2010
    Location
    Norway
    Beans
    674

    Re: How do you find the Directory of the open Shell Script File?

    Code:
    cp -r $mydir /location/dir

  7. #7
    Join Date
    Oct 2010
    Location
    The Big, Bad, World!
    Beans
    120
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Thumbs down Re: How do you find the Directory of the open Shell Script File?

    Quote Originally Posted by Smart Viking View Post
    Code:
    cp -r $mydir /location/dir
    Spot on mate . I'm so bad at shell, just need to know this little thing for a small script. Then how does that paste into another folder?
    Choose Freedom, Choose Linux, Choose Ubuntu.

  8. #8
    Join Date
    Oct 2010
    Location
    The Big, Bad, World!
    Beans
    120
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Thumbs down Re: How do you find the Directory of the open Shell Script File?

    Quote Originally Posted by ofnuts View Post
    $PWD gives you the current directory, which isn't the directory where the shell script file is (can be invoked with an explicit path, or be somewhere in the PATH). The directory where the script comes from is obtained by:
    Code:
    mydir=$(dirname $0)
    In other words:
    Code:
    #! /bin/bash
    
    me=$0
    mydir=$(dirname $0)
    
    echo "Me: $me"
    echo "My dir: $mydir"
    Hey, I was getting errors, so I tried echo "hello: $mydir" - then it just printed out a plain line - as if there was no Directory?
    Choose Freedom, Choose Linux, Choose Ubuntu.

  9. #9
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: How do you find the Directory of the open Shell Script File?

    In many cases $mydir is just a mere dot: ".".

  10. #10
    Join Date
    Oct 2010
    Location
    The Big, Bad, World!
    Beans
    120
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Thumbs down Re: How do you find the Directory of the open Shell Script File?

    Quote Originally Posted by ofnuts View Post
    In many cases $mydir is just a mere dot: ".".
    So, I don't understand, could you walk me through how to set it up, so that the folder in which the shell script is located, is copied to a different folder?
    Choose Freedom, Choose Linux, Choose Ubuntu.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •