Cd to script-directory

From FVue
Jump to: navigation, search

Change to the directory where the main script resides. Or: How to make sure the current working directory is the directory from where the script is running?

Bash

    # cd to script dir
cd "$(dirname "$(type -p "$0")")"
scriptDir="$PWD"

See also

Finding absolute path of a script from within
Same question in forum, followed by a two page discussion. Dated Mar, 2008.

Tcl

    # cd to script dir
if {[info tclversion] >= 8.4} {
    set scriptDir [file normalize [file dirname [info script]]]
} else {
    append scriptDir [pwd] / [file dirname [info script]]
}; # if
cd $scriptDir

Php

    // cd to script dir
$script_dir = realpath(dirname(__FILE__));
chdir($script_dir)

Journal

20080116

Unix Shell - bash: path of script that is being executed
Discussion on webservertalk.com dated Sep-2006 regarding bash

Comments

blog comments powered by Disqus