3

While going through:

info coreutils 'dd invocation'

I came across:

dd if=/dev/zero of=/dev/null count=10MB & pid=$!

What is $! used for?

goldilocks
  • 86,451
  • 30
  • 200
  • 258
Bleeding Fingers
  • 971
  • 3
  • 12
  • 23

2 Answers2

13

If you're talking about Bash they're in the "Special Parameters" section of the Bash man page.

!      Expands to the process ID of the most recently executed background 
       (asynchronous) command.

Example

$ sleep 10 &
[1] 22257

$ echo $!
22257

Your command

So with this command:

$ dd if=/dev/zero of=/dev/null count=10MB & pid=$!

The dd command is backgrounded, and the resulting process ID ($!) is stored in a variable pid for use there after.

References

slm
  • 363,520
  • 117
  • 767
  • 871
6

$! is used to get the PID(process identifier) of the most recent background command.

There is also !$:

!$ is used to get last argument for the last executed command.

slm
  • 363,520
  • 117
  • 767
  • 871