I’m reading Wrox, Beginning Linux Programming.
But I’ve got a question about shell variables.
Here is a shell script named test.sh:
#! /bin/bash
read test
echo $test
exit 0
Then I chmod the script:
$ chmod +x test.sh
$ ./test,sh
When I type “ok”, it echos the “ok”.
But when I returned to the shell interface, I typed:
$ echo $test
It returned nothing…
I don’t know why $test doesn’t return “ok” just like the script does…
And the book doesn’t metion it…
Thanks in advance:)
The variables only exist in the process where they were created (and also in child processes, if they are exported). Running a script creates a new process, after the script ends, all its variables are lost.
You can run the script without starting a new process by “sourcing” it:
You should remove the
exitthen, though, otherwise it will exit your current shell.