Find the answer to your Linux question:
Results 1 to 6 of 6
I'm currently studying BASH and can not see the connection of using "./". I assume it is supposed to represent my home directory but it will not work. I can ...
  1. #1
    Linux Newbie theKbStockpiler's Avatar
    Join Date
    Sep 2010
    Location
    Upstate NY
    Posts
    195

    Use of ./ in BASH Etc.



    I'm currently studying BASH and can not see the connection of using "./". I assume it is supposed to represent my home directory but it will not work. I can not use it to move a directory either like ../ but I do not have a guide that describes much at all.

    Thanks in advance!

  2. #2
    Linux Newbie Nagarjuna's Avatar
    Join Date
    Feb 2011
    Posts
    122
    If I'm not mistaken, a ./ represents your present working directory. In other words, the directory in which the script is located/working from.

    If your script is located in /home/foo/scripts/bash_script, then adding a ./ to your script would be a shorter way of referencing that directory.

    So a single dot usually always references your present working directory. Two dots usually the directory just above your present working directory. A ~/ should reference your home directory, and when used in a script, I believe it references the home directory of the owner of that script.

    Someone please correct me if I'm wrong.

    I hope that helps! Enjoy your scripting

  3. #3
    Linux Newbie theKbStockpiler's Avatar
    Join Date
    Sep 2010
    Location
    Upstate NY
    Posts
    195

    Thanks for the Reply!

    While using this version

    [kbs@localhost ~]$ /bin/bash --version
    GNU bash, version 4.1.2(1)-release (i386-redhat-linux-gnu)
    I don't have to use a "./" to address a relative path so I don't know what other reason the statement "./" is for.

  4. #4
    Just Joined!
    Join Date
    Mar 2011
    Location
    Wales
    Posts
    15
    Afternoon, chaps.

    The reason for the ./ thing is that it'll run a script in the current directory even if the latter is not in your path.

    Type

    Code:
    echo $PATH
    If you see the period (.) somewhere in your path, for example like this...

    Code:
    /home/paul/bin:.:/usr/local/bin:/usr/bin:/bin:/usr/share/texmf/bin
    ...then your current directory is in your path, which means you can run a script which is in your current directory without having to prefix its name with ./. Prefixing the name of your script with ./ is a fail-safe measure which ensures that the thing'll run even if the current directory's not in your path. (Folk use it when helping others or writing instructions in case the reader's path doesn't include the current directory.)

    Note that the current directory isn't usually added to root's path variable for security reasons. (Use the first command above to compare root's path with an ordinary user's path.)

    And Nagarjuna:

    Someone please correct me if I'm wrong
    You're not wrong --- what you said was all quite right.

    Cheerio!

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I'd like to expand on Ignotum's post a bit.

    First of all, adding '.' to PATH is generally considered to be a bad idea from a security perspective, for every user. The reason for this is that it allows any program to possibly create an executable called, for instance, "ls", that might be executed before the proper "ls", depending on how your PATH is configured. In general, you should use standard directories for your PATH (and you can include your own directories, as long as they are static), and then if you are executing anything not in one of those directories, you give the path.

    Anyway, the whole './' prefix is, as has been said, in case the executable is not in your PATH. When you type a command into a shell, the first thing it does is checks if the command is simply a command name ("ls") or a path ("/bin/ls"). If it is a command name, PATH is searched. If it is a path, then only the file at that path is executed.

    So if the command is in a subdirectory, you could always say "foo/bar/exe". However, if the executable is in the current directory, "exe" is not sufficient, as this is not a path. Therefore, "./exe" is a path that points to the current directory.
    DISTRO=Arch
    Registered Linux User #388732

  6. #6
    Linux Newbie theKbStockpiler's Avatar
    Join Date
    Sep 2010
    Location
    Upstate NY
    Posts
    195
    Thanks for sharing your expertise everyone. All of it has not sunken in but I think I grasp most of it.

Posting Permissions

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