Quantcast
Channel: How to determine the path to a sourced tcsh or bash shell script from within the script - Unix & Linux Stack Exchange
Browsing all 35 articles
Browse latest View live

Answer by HemanthJabalpuri for determining path to sourced shell script

wdir="$PWD"; [ "$PWD" = "/" ] && wdir="" case "$0" in /*) scriptdir="${0%/*}";; *) scriptdir="$wdir/${0#./}"; scriptdir="${scriptdir%/*}";; esac echo "$scriptdir" Maybe this will not work with...

View Article



Answer by Patrick Maupin for determining path to sourced shell script

this answer describes how lsof and a bit of grep magic is the only thing that seems to stand a chance of working for nested sourced files under tcsh: /usr/sbin/lsof +p $$ | grep -oE /.\*source_me.tcsh

View Article

Answer by JamesThomasMoon1979 for determining path to sourced shell script

tl;dr script=$(readlink -e -- "${BASH_SOURCE}") (for bash obviously) $BASH_SOURCE test cases given file /tmp/source1.sh echo '$BASH_SOURCE '"(${BASH_SOURCE})" echo 'readlink -e $BASH_SOURCE'\...

View Article

Answer by dols3m for determining path to sourced shell script

To make your script both bash- and zsh-compatible instead of using if statements you can simply write ${BASH_SOURCE[0]:-${(%):-%x}}. The resulting value will be taken from BASH_SOURCE[0] when it's...

View Article

Answer by Mathieu CAROFF for determining path to sourced shell script

I was a bit confused by the community wiki answer (from Shawn J. Goff), so I wrote a script to sort things out. About $_, I found this: Usage of _ as an environment variable passed to a command. It's...

View Article


Answer by Paul Brannan for determining path to sourced shell script

This worked for me in bash, dash, ksh, and zsh: if test -n "$BASH" ; then script=$BASH_SOURCE elif test -n "$TMOUT"; then script=${.sh.file} elif test -n "$ZSH_NAME" ; then script=${(%):-%x} elif test...

View Article

Answer by gkb0986 for determining path to sourced shell script

This solution applies only to bash and not tcsh. Note that the commonly supplied answer ${BASH_SOURCE[0]} won't work if you try to find the path from within a function. I've found this line to always...

View Article

Answer by Matt for determining path to sourced shell script

For the bash shell, I found @Dennis Williamson's answer most helpful, but it didn't work in the case of sudo. This does: if ( [[ $_ != $0 ]] && [[ $_ != $SHELL ]] ); then echo "I'm being...

View Article


Answer by Shawn J. Goff for determining path to sourced shell script

For thoroughness and the sake of searchers, here is what these do... It is a community wiki, so feel free to add other shell's equivalents (obviously, $BASH_SOURCE will be different). test.sh: #!...

View Article


Answer by Dennis Williamson for determining path to sourced shell script

In tcsh, $_ at the beginning of the script will contain the location if the file was sourced and $0 contains it if it was run. #!/bin/tcsh set sourced=($_) if ("$sourced" != "") then echo "sourced...

View Article

Answer by Bruce Ediger for determining path to sourced shell script

Actually, "dirname $0" will get you the path to the script, but you have to interpret it a bit: $ cat bash0 #!/bin/bash echo \$0=$0 dirname $0 $ bash0 # "." appears in PATH right now. $0=./bash0 . $...

View Article

Answer by pbm for determining path to sourced shell script

I think that you could use $BASH_SOURCE variable. It returns path that was executed: pbm@tauri ~ $ /home/pbm/a.sh /home/pbm/a.sh pbm@tauri ~ $ ./a.sh ./a.sh pbm@tauri ~ $ source /home/pbm/a.sh...

View Article

determining path to sourced shell script

Is there a way for a sourced shell script to find out the path to itself? I'm mainly concerned with bash, though I have some coworkers who use tcsh. I'm guessing I may not have a ton of luck here,...

View Article


Answer by maoizm for determining path to sourced shell script

The trickiest part is finding currently sourced file is for dash shell used as sh replacement in Ubuntu. The following code snippet can be used in the script being sourced to determine its absolute...

View Article

Answer by gen1us for determining path to sourced shell script

If you need absolute path to the directory containing the script, you can use this snippet:BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]:-$0}" )" >/dev/null 2>&1 && pwd )"If you only...

View Article


Answer by Pablo Almaguer for determining path to sourced shell script

I use the code above to get the path of sourced bash file:$(readlink -f ${BASH_SOURCE[0]})

View Article

Answer by Hutch for determining path to sourced shell script

mention these two lines inside your main script,SCRIPT=`realpath $0`SCRITPATH=`dirname $SCRIPT`other script should be in the same folderto use them,bash $SCRITPATH/your_sub_script.sh #(this line also...

View Article


Answer by Gabriel Staples for determining path to sourced shell script

Here is the best answer I think. It solves a lot of problems not solved in the other answers, namely:realpath can find the absolute path from a relative path, and will expand symbolic links (use...

View Article

Answer by Hutch for How to determine the path to a sourced tcsh or bash shell...

mention these two lines inside your main script,SCRIPT=`realpath $0`SCRITPATH=`dirname $SCRIPT`other script should be in the same folderto use them,bash $SCRITPATH/your_sub_script.sh #(this line also...

View Article

Answer by Gabriel Staples for How to determine the path to a sourced tcsh or...

Here is the best answer I think. It solves a lot of problems not solved in the other answers, namely:realpath can find the absolute path from a relative path, and will expand symbolic links (use...

View Article
Browsing all 35 articles
Browse latest View live




Latest Images