Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the location of the tcsh shell script I'm executing?

Say I put an executable tcsh file in /path/to/my_script.csh

and my current directory is anywhere, for example I'm in /path

So I type to/my_script.csh

I want to have a line in my_script.csh that will return "/path/to/my_script.csh" - like ruby's

__FILE__
like image 811
Julian Mann Avatar asked Apr 01 '10 20:04

Julian Mann


2 Answers

In c shell, try like this:

set rootdir = `dirname $0`
set abs_rootdir = `cd $rootdir && pwd`
echo $abs_rootdir
like image 65
euccas Avatar answered Sep 18 '22 15:09

euccas


If you want to ensure the same result (full path and script name) try something like this:

...
rootdir=`/bin/dirname $0`       # may be relative path
rootdir=`cd $rootdir && pwd`    # ensure absolute path
zero=$rootdir/`/bin/basename $0`
echo $zero
...

Then you can call it as foo.sh, ./foo.sh, some/lower/dir/foo.sh and still get the same result no matter how it is called.

like image 38
DAS Avatar answered Sep 19 '22 15:09

DAS