1

if i use the dot-command from bash (or "source" ) it executes TWICE ===== demo === ( for import ) ====== (saved as "demo")

x+="|"
subResTest() {
    return 0 # do nothing 
}   
echo "HI"$x

====== test ==== ( same directory as demo )

#/bin/bash
. demo
for ((i=1 ; i<10 ; i++)) ; do
    echo -n $i$x 
    subResTest
done
echo

====== Result =====

./test
HI|
HI||
1||2||3||4||5||6||7||8||9||

================ So what is going on ? The problem is, if something should only executes once per import it leads to wrong results - even in this simple example.

Running System : Mx Linux 19.2 (debian-based) bash 5.0.3(1) test starts from an xfce4-terminal

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 4
    i cannot confirm – alecxs Jul 19 '20 at 13:16
  • if it woulld be a generic proble, others would have run into the same thing. Maybe someone has a idea what is causing this . Still testing it for myself ;-) – Christian Schell Jul 19 '20 at 13:18
  • yeah, my FIRST entry, and a RTFineM - i'll do it (if i get the time) I was trying to get at least a start with the problem itself before learning text-editing – Christian Schell Jul 19 '20 at 13:24
  • Change the shebang of `test` into `#!/bin/bash -x` (to enable tracing) and have a look at the output of `./test`. Or do `strace -efile ./test` to have a look at all the files that are opened by `bash` – Hans Lub Jul 19 '20 at 13:41
  • there is no path for demo you probably sourced multiple paths. it works fine with `. ./demo` (and shouldn't have demo shebang too?) – alecxs Jul 19 '20 at 13:42
  • 2
    You probably (when testing) did `. test` multiple times. As a result, `x` got concatenated with `|` multiple times *in* *the* *current* *shell* (in your terminal). That explains your result. You could do `unset x`, and test the whole setup again. But if your aim is to modify `x` in the current shell, do not be surprised at this behaviour; you wanted it, you got it. –  Jul 19 '20 at 14:07
  • 2
    In short, *sourcing* modifies the *current* shell. –  Jul 19 '20 at 14:12
  • Thanks for your comments - i did try running test fpr multiply time, but each time it ended up with "doubles" . ich inserted the +!" for tracing "how many times" – Christian Schell Jul 19 '20 at 16:33
  • And after cleaning up ALL other scripts with imports - the error vanished - i decided to remove all calls "from inside the imported file" to itself ( like initfunc(){...] initfunc changed to source file ; initfunc ). Also all variables are initialised in that initfunc ans not outside .Maybe there are some strange behaviour from bash with sourced ressources – Christian Schell Jul 19 '20 at 16:37
  • i even logged out from herbstluftwm to fluxbox to check if its some of my other scripts for that herbstluftwm causing the trouble – Christian Schell Jul 19 '20 at 16:41
  • Hello - If you dont add to x then all things fine. Make only ```x='|'``` So next sourcing dont add a | to a existing x. – koyaanisqatsi Jul 19 '20 at 19:55

0 Answers0