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

后端 未结 4 1169
攒了一身酷
攒了一身酷 2021-02-20 05:10

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

4条回答
  •  醉酒成梦
    2021-02-20 05:45

    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.

提交回复
热议问题