[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: whats the correct way to check if current is being sourced or not
From: |
Greg Wooledge |
Subject: |
Re: whats the correct way to check if current is being sourced or not |
Date: |
Thu, 15 Aug 2024 15:16:00 -0400 |
On Thu, Aug 15, 2024 at 21:03:24 +0200, alex xmb sw ratchev wrote:
> On Thu, Aug 15, 2024, 02:21 Greg Wooledge <greg@wooledge.org> wrote:
>
> > On Thu, Aug 15, 2024 at 01:53:03 +0200, alex xmb sw ratchev wrote:
> > > plz ?
> >
> > https://mywiki.wooledge.org/BashFAQ/109
> dood
> the funcname stuff , in ur url
> my funcname ( [@] ) is all empty
Here's a file:
hobbit:~$ cat foo
#!/bin/bash
sourced() {
declare -p FUNCNAME
}
sourced
Running (not sourcing):
hobbit:~$ ./foo
declare -a FUNCNAME=([0]="sourced" [1]="main")
Sourcing:
hobbit:~$ bash
hobbit:~$ source ./foo
declare -a FUNCNAME=([0]="sourced" [1]="source")
hobbit:~$ exit
exit
Sourcing from a much older bash:
hobbit:~$ bash-3.2
bash-3.2: /home/greg/.bashrc: line 95: syntax error in conditional expression:
unexpected token `('
bash-3.2: /home/greg/.bashrc: line 95: syntax error near `+(['
bash-3.2: /home/greg/.bashrc: line 95: ` if [[ $letters != +([A-Z]) ]]; then'
hobbit:~$ source ./foo
declare -a FUNCNAME='([0]="sourced" [1]="source")'
hobbit:~$ exit
exit
What version of bash are you using, what does your file look like, how
are you running or sourcing it, and what results do you actually get?
You DID remember to read the part that says,
Now, since this is bash, nothing can ever be straightforward. This
variable only exists when you're inside of an actual function. So
you can't do this check in the main body of the script. You have
to write a function, call it, and peek one slot down in the call
stack to see where you were before the function was called.
Right?