Pid=$!

Hello,

I would like to know what this command means?

PID=$!

what does "!" stand for?

That's not in my version of bash or korn shell. You know you should just do echo $!

$!  Process number of last background command.

More information here, or just lookup Built-in Bash Shell Variables.

1 Like

it gives me null value..:confused:, btw this is on an AIX machine.

---------- Post updated at 03:10 PM ---------- Previous update was at 03:09 PM ----------

Thanks :slight_smile:

You must've overlooked it. The ! built-in variable has been around for a long time.

Regards,
Alister

---------- Post updated at 05:42 PM ---------- Previous update was at 05:40 PM ----------

It's null until you run something in the background with & or bg .

Regards,
Alister

Welcome googlietdr,

The value assigned to $! is the process id of the last command started in the background from the current shell.

It can be useful if you want to do something like this:-

(sleep 1
while true
do
   echo ".\c"
   sleep 1
done) &
SLEEP_PID=$!

echo "Copying file to local disk...\c"
cp enournmous_NFS_file new_local_file
RC=$?
kill $SLEEP_PID

if [ $RC -ne 0 ]
then
   errors
fi
:
:
etc.

Whilst the copy is running, the user will get a row of dots growing by one each second. It makes them think something is happening when they see a display moving. It can be a useful filler to stop them cancelling mid-way. I've fixed a few 'intermittent' problems with just such a placebo.

Of course, you could have a more processing efficient action to take and it allows a little bit of multi-threading of your script, or you can have a script that starts various sub-processes and you could collect the process id from each, then periodically check that they are still running.

I hope that this explains a little. Feel free to ask more.

Robin
Liverpool/Blackburn
UK

1 Like