Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get path of current script?

Tags:

tcl

Sometimes its needed to get current path of a script. What are the ways to do that?

like image 348
vasili111 Avatar asked Apr 25 '14 06:04

vasili111


People also ask

How do I find the script path?

In this case, first, we need the current script's path, and from it, we use dirname to get the directory path of the script file. Once we have that, we cd into the folder and print the working directory. To get the full or absolute path, we attach the basename of the script file to the directory path or $DIR_PATH.

How do I get the current path in Python?

getcwd() method is used for getting the Current Working Directory in Python. The absolute path to the current working directory is returned in a string by this function of the Python OS module.

How do I get current path in bash?

Print Current Working Directory ( pwd ) To print the name of the current working directory, use the command pwd . As this is the first command that you have executed in Bash in this session, the result of the pwd is the full path to your home directory.


1 Answers

While a script is being evaluated (but not necessarily while its procedures are being called) the current script name (strictly, whatever was passed to source or the C API equivalent, Tcl_EvalFile() and related) is the result of info script; it's highly advisable to normalise that to an absolute pathname so that any calls to cd don't change the interpretation.

Scripts that need the information tend to put something like this inside themselves:

# This is a *good* use of [variable]…
variable myLocation [file normalize [info script]]

They can then retrieve the value (or things derived from it) easily:

proc getResourceDirectory {} {
    variable myLocation
    return [file dirname $myLocation]
}

The other common locations are:

  • $::argv0 which is the “main” script (and you're evaluating the main script if it's equal to info script)
  • [info nameofexecutable] which is the Tcl interpreter program itself (typically the argv[0] at the C level)
  • [info library] which is where Tcl's own library scripts are located
  • $::tcl_pkgPath which is a Tcl list of directories where packages are installed
  • $::auto_path which is a Tcl list of directories where scripts are searched for (including packages! The package path is used to initialise this.)
like image 57
Donal Fellows Avatar answered Nov 02 '22 07:11

Donal Fellows