2

I am unable to make even the simplest export of variables, from within scripts, to work in my bash - what am I dooing wrong?

File test.sh :

#!/bin/bash
echo $ttt
ttt="fffalse"
export ttt
echo $ttt

bash test :

hpek@hpek:~/temp$ export ttt="tttrue"
hpek@hpek:~/temp$ ./test.sh 
tttrue
fffalse
hpek@hpek:~/temp$ ./test.sh 
tttrue
fffalse
hpek@hpek:~/temp$ 

Edit:

I now know from the answers, that this will not work. -but how can make a single variable remembered between processes? Do I need to store it in a file?

1
  • +1 for the title. Bad bash! :))) Commented May 5, 2012 at 1:11

2 Answers 2

4

./test.sh is the same as bash test.sh

Each shell script running is, in effect, a subprocess (child process) of the parent shell.
And subprocess cannot export env-var to it's parent.


You can try this(run in the same environment):

. test.sh
Sign up to request clarification or add additional context in comments.

2 Comments

". test.sh" is actually working - thanks. What does this mean? that is is run in current shell and not as a child?
@Hans-PeterE.Kristiansen . test.sh is completely equivalent to source test.sh which just means that the interpreter takes each line from test.sh and runs it in the current environment as if you had typed it into the current script. If you're familiar with C, it's somewhat similar to what the preprocessor does when you do #include "file.h". see this link: ( ss64.com/bash/period.html )
3

export works in the current process and any children spawned afterward; it does not work across process boundaries (parents, existing children, unrelated processes). The environment behaves like a sort of shadow argument list, not like a filesystem or mailbox.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.