When you run the script with:
you're starting an entirely separate
bash process and putting it in the background. You can't see the variables defined in that script; they belong to the other process. If you want to get access to variables defined in another file, load that file into your current
bash process using the
After that, you'll be able to say
$pid in your main script and get the value that was set in the other file.
All of that said — the way you're structuring things is very puzzling. The script you've posted has a lot of other errors that I assume are from editing the real one down for the question (you use
$! as though it's an
awk variable, but then expect the variable you copied it into to be defined in the Bash script, for example, and
test0 would have many syntax errors).
$! is only defined in Bash after a background process is run with &: even if your
awk command worked,
$pid would be empty. If you want to get a hold of the
xterm process's PID in that way, you need to run it as
xterm ... &.
bash /tmp/test0 & puts the
test0 script in the background, rather than the
It seems like you'd be better off getting rid of
test0 altogether, and just launching
xterm from the main script. It's probably worth having a rethink about how you're going about things at a higher level. At the very least, look at what's in your real
test0 file and see whether it's what you actually want.