Timeline for How to determine the path to a sourced tcsh or bash shell script from within the script
Current License: CC BY-SA 4.0
24 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 28, 2022 at 22:14 | comment | added | Gabriel Staples | It seems like this answer isn't answering the question at all (determining the path to a sourced shell script--I've just added an answer on that here), but instead is answering this totally other question: How to detect if a script is being sourced [versus run]. If that's what is desired, here is a summary I have written of ways to do that too. | |
| Aug 28, 2018 at 0:27 | comment | added | Dennis Williamson | @nispio: I've made a change that seems to work in all cases (except sourcing it from tcsh which won't work because the syntax is completely different). Thanks for the report. | |
| Aug 28, 2018 at 0:27 | history | edited | Dennis Williamson | CC BY-SA 4.0 | fixed Bash version |
| Aug 27, 2018 at 22:08 | comment | added | nispio | @DennisWilliamson I am talking about a tcsh user who decides to execute my bash script. This is a very normal use case, but is not supported by the bash example above. | |
| Aug 25, 2018 at 2:25 | comment | added | Dennis Williamson | @nispio That's why there are two separate examples - one for Bash and one for tcsh. | |
| Aug 24, 2018 at 23:56 | comment | added | nispio | The bash example works when being executed from bash, but not from tcsh. For an example, try tcsh -c '/path/to/myscript.sh' | |
| Mar 15, 2017 at 23:40 | comment | added | Dennis Williamson | @PaulBrannan: It's documented in the Special Parameters section of the manual and in the corresponding section of the man page. It's shown as an underscore without the dollar sign which makes it hard to search for. The reason you get the last line of your ~/.bashrc is because that file is effectively sourced so the "Subsequently, expands to the last argument to the previous command, after expansion." behavior comes into play (which would be the command itself if there are no arguments). | |
| Mar 15, 2017 at 13:58 | comment | added | Paul Brannan | Using bash 4.3.11, "echo $_" as the first line of my script prints the last line of my bashrc when running in a fresh shell. I see no mention of $_ in the bash manpage except in regard to MAILDIR. In zsh, $_ seems to always be equal to $0. | |
| Nov 13, 2015 at 23:03 | comment | added | KingPong | I had to use [[ "${called##*/}" != "${0##*/}" ]] for bash because $_ contains the absolute path. This hack essentially compares basenames. | |
| Jul 11, 2015 at 12:48 | comment | added | Dilawar | If you use #/usr/bin/env bash instead of #!/bin/bash in caller script, this wouldn't work. | |
| Apr 1, 2014 at 8:19 | comment | added | reinierpost | @Dennis Williamson: The tcsh solution prints the pathname of the executing/sourcing script, not the sourced one. The bash solution doesn't work ($_ and $BASH_SOURCE do not seem to exist) if the executing/sourcing script is run with /bin/sh, even when /bin/sh is bash. | |
| Mar 28, 2014 at 15:29 | comment | added | Dennis Williamson | @reinierpost: You don't say which one doesn't work (Bash or tcsh) or in what way it doesn't. In Bash, the $BASH_SOURCE array contains the chain of callers. | |
| Mar 28, 2014 at 12:59 | comment | added | reinierpost | This doesn't work to find the location of a script sourced by another script! | |
| Mar 5, 2013 at 7:35 | comment | added | clacke | Haha. Obviously I was testing by first doing source, then doing .. I apologize for being incompetent. They are indeed identical. Anyway, $BASH_SOURCE works. | |
| Mar 4, 2013 at 11:51 | comment | added | Dennis Williamson | @clacke: I find that in all the versions of Bash that I tested from 2.05b to 4.2.37, including 4.1.9, that . and source worked identically in this regard. Note that $_ must be accessed in the first statement in the file, otherwise it will contain the last argument of the previous command. I like to include the shebang for my own reference so I know what shell it's supposed to be for and for the editor so it uses syntax highlighting. | |
| Mar 4, 2013 at 11:14 | comment | added | clacke | Interesting. In bash 4.1.2, sourcing with source does not set $_, but sourcing with . does. | |
| Mar 4, 2013 at 11:08 | comment | added | clacke | If the file is meant to be sourced, it is probably best to not have the shebang and to not set the file executable. | |
| May 10, 2011 at 16:55 | comment | added | Cascabel | @Dennis: Yeah, I'd certainly prefer not to deal with this. But my workplace uses a simulation framework which is completely dependent on environment variables for building (OUR_CFLAGS kinds of things), so you source a script to get them set up (or you'd never have a prayer of getting it to build), and everything uses absolute paths, and.... I think you get the idea. | |
| May 10, 2011 at 15:35 | comment | added | Dennis Williamson | ...the data file in a fixed location rather than trying to come up with a method of determining where the script lives and accessing the data (or config, etc.) relative to that. | |
| May 10, 2011 at 15:33 | comment | added | Dennis Williamson | Sourcing it works for me without the shebang. > tcsh --version\n tcsh 6.14.00 (Astron) 2005-03-25 (i486-intel-linux) options wide,nls,dl,al,kan,rh,nd,color,filec. As far as sourcing it non-interactively, the source file is included into the parent file as if it were actually a part of it (indistinguishably so) as you mention in your original question. I think your positional parameter workaround is probably the best approach. However, the usual question is "why do you want to do that" and the usual answer to the reply is "don't do that - do this instead" where "this" is often to store... | |
| May 9, 2011 at 18:04 | comment | added | Cascabel | The tcsh version also doesn't seem to work if the script is sourced noninteractively (e.g. from a cshrc). I can't seem to find a way to get the information in that case. Any thoughts? | |
| Apr 20, 2011 at 16:28 | comment | added | Cascabel | I just had occasion to use this in tcsh, and noticed that it doesn't work without the shebang. Seems a bit odd for the behavior to change if you're just sourcing it, not executing it... | |
| Dec 8, 2010 at 19:44 | vote | accept | Cascabel | ||
| Dec 8, 2010 at 19:27 | history | answered | Dennis Williamson | CC BY-SA 2.5 |