Quantcast
Channel: How to determine the path to a sourced tcsh or bash shell script from within the script - Unix & Linux Stack Exchange
Browsing index pages (35 articles)
↧

How to determine the path to a sourced tcsh or bash shell script from within...

Is there a way for a sourced shell script to find 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 much luck here since sourcing...

View Article


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

I think that you could use $BASH_SOURCE variable. It returns path that was executed:Example script:print_script_path.sh:#!/usr/bin/env bashecho "$BASH_SOURCE"Make it executable:chmod +x...

View Article


Answer by Dennis Williamson for How to determine the path to a sourced tcsh...

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/tcshset sourced=($_)if ("$sourced" != "") then echo "sourced...

View Article

Answer by Shawn J. Goff for How to determine the path to a sourced tcsh or...

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 Matt for How to determine the path to a sourced tcsh or bash shell...

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 gkb0986 for How to determine the path to a sourced tcsh or bash...

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 Paul Brannan for How to determine the path to a sourced tcsh or...

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

View Article

Answer by Mathieu CAROFF for How to determine the path to a sourced tcsh or...

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 dols3m for How to determine the path to a sourced tcsh or bash...

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 JamesThomasMoon for How to determine the path to a sourced tcsh or...

tl;drscript=$(readlink -e -- "${BASH_SOURCE}") (for bash obviously)$BASH_SOURCE test casesgiven file /tmp/source1.shecho '$BASH_SOURCE '"(${BASH_SOURCE})"echo 'readlink -e $BASH_SOURCE'\"($(readlink -e...

View Article

Answer by Patrick Maupin for How to determine the path to a sourced tcsh or...

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 HemanthJabalpuri for How to determine the path to a sourced tcsh or...

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

View Article

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

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 How to determine the path to a sourced tcsh or bash...

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 How to determine the path to a sourced tcsh or...

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

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

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 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 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 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
Browsing index pages (35 articles)


Latest Images