I have 2 scripts:
script: script1.sh
#!/bin/bash [[ $0 = "$BASH_SOURCE" ]] && { echo "You must source me!"; exit; } if [ -z ${I} ]; then echo echo -n "some string: " stty -echo read I #export I stty echo echo else echo "ALREADY SET!!!" fi echo "--- $I"script: script2.sh
#!/bin/bash echo "--- $I" if [ -z $I ]; then echo "VARIABLE NOT SET" else echo "VARIABLE SET" fi
And now the part that confused me... I first run script1 and then script2...
First scenario:
As you can see I can print $I variable (value: asd) inside script1. When I run script2 it returns "VARIABLE NOT SET", but when I try to echo $I I get "asd"
wolfy@VMtest:~$ . ./script1.sh
some string:
--- asd
wolfy@VMtest:~$ ./script2.sh
---
VARIABLE NOT SET
wolfy@VMtest:~$ echo $I
asd
wolfy@VMtest:~$
Second scenario:
Now I uncomment #export I and rerun both scripts as before (before rerun I created a new session so that all variables are reseted)
In this case I can read $I in script2 and echo it
wolfy@VMtest:~$ . ./script1.sh
some string:
--- asd
wolfy@VMtest:~$ ./script2.sh
--- asd
VARIABLE SET
wolfy@VMtest:~$ echo $I
asd
Can someone explain me why in my first scenario I can echo $I, but I can't use it in second script?