13

I have been studying for my Unix exam and I often saw exclamation mark variable - $! If I write echo $! on my Mac shell tells me something like "You have mail in…"

Is purpose of this to store whether I do or do not have any mails or is there something else?

EDIT: Funny coincidence happened. Exactly when I wrote echo $! my Shell also notified me that I have some mail. So that's why I wrote that mail part.

0

3 Answers 3

19

$! is the PID of the most recent background command.

See this excellent answer for other special parameters.

6

The shell checks for new mail after a command finished and $MAILCHECK or 600 seconds have passed.

echo $! prints the PID of the last background process. After that, a check for mail may happen.

5

$! is replaced by the process identifier of the last process placed in the background in the current shell, if any. The bash manual provides details of all such special parameters.

To see this in action:

echo $!

should print 0 (assuming no jobs have been backgrounded)

man bash &
echo $!

should print the process identifier of the man process which is in the background. The full output should look something like

$ man bash &
[1] 4319
[1]  + 4319 suspended (tty output)  man bash
$ echo $!
4319

Not the answer you're looking for? Browse other questions tagged or ask your own question.