sh equivalent of __FILE__
Is there a sh equivalent of __FILE__
, to give me the pathname of the currently executing file? POS开发者_StackOverflow中文版IX solutions preferred, bash acceptable, thanks.
Try using $0
.
This will grab both the file and the directory
# !/bin/sh
# store where we are
__PWD__=$(pwd)
# go to the current file (i.e. this file)
cd $(dirname $0)
# gives the file name, __FILE__
echo $0
__FILE__=$0
# gives the directory name, __DIR__
echo $(dirname $0)
__DIR__=$(dirname $0)
# this has been a test of the Linux filesystem; we now return you to your previous program.. :)
cd $__PWD__
For a bash script solution
Getting the source directory of a Bash script from within
Just a thought:
#!/usr/bin/env bash
# "$0" will expand to the name of the script, as called from the command line
readlink -f $0
精彩评论