Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sh equivalent of __FILE__

Tags:

file

shell

php

Is there a sh equivalent of __FILE__, to give me the pathname of the currently executing file? POSIX solutions preferred, bash acceptable, thanks.

like image 398
pilcrow Avatar asked Jul 23 '10 14:07

pilcrow


People also ask

What is $_ in shell script?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails. $@

What is $Bash_source?

BASH_SOURCE. An array variable whose members are the source filenames where the corresponding shell function names in the FUNCNAME array variable are defined. The shell function ${FUNCNAME[$i]} is defined in the file ${BASH_SOURCE[$i]} and called from ${BASH_SOURCE[$i+1]}


2 Answers

Try using $0.

like image 166
lhf Avatar answered Nov 02 '22 16:11

lhf


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__
like image 43
user12656390 Avatar answered Nov 02 '22 16:11

user12656390