I would like to kill the xterm process I started this way in a script:


echo "#!/bin/bash" > /tmp/test0
grep file | awk '{print "xterm -hold -e echo",$1,"sleep 10" && pid=$!}' >> /tmp/test0
echo "exit" >> /tmp/test0
chmod 700 /tmp/test0
bash /tmp/test0&
sleep 5
kill $pid  ---> does not work

now let's say, I have got several xterm launched this way in the same bash (test0), is there a way to store their corresponding pid in an array and call them back when it's time to kill them? Furthermore, when storing these values, is there a way to associate them with another value (in this example with $1) to make it easier to call back. nb: $1 = text

thank you very much !

2 Answers 2


When you run the script with:

bash /tmp/test0&

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 . or source command:

. /tmp/test0

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 xterm command.

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.

  • IFS=',' tab=($(grep pattern file | awk '{print "xterm -hold -e echo something",$1,"&",","}')) for i in "${tab[@]}" do : eval $i done IFS=$OIFS b=$(grep pattern file | wc -l) i=0 ps au | grep xterm | awk '{print $2}' | while read -r PID && [[ $i -ne $b ]] do ARRAY[$i]=$PID echo "ARRAY[$i]=" echo ${ARRAY[$i]} ((i++)) done; sure it could be smarter but it does work !
    – blue_xylo
    Aug 17, 2014 at 14:29

(Your bash script doesn't run, there are errors).

So, why is the pid not killed? Is it the wrong pid? Or maybe do you need to force it's killing using kill -9?

Then, use pgrep to get the pids. Create a new xterm and store the pid in xtermPid_1:

$ xtermPid_1=$(pgrep --newest xterm)

(look at the man pages of pgrep, it's a nice tool)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .