Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

While going through:

info coreutils 'dd invocation'

I came across:

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

What is $! used for?

share|improve this question

marked as duplicate by Mikel, manatwork, slm, Renan, Anthon Oct 23 '13 at 16:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Are you using bash? – slm Oct 22 '13 at 20:02
    
@slm how am I supposed to find out? – Bleeding Fingers Oct 22 '13 at 20:05
    
You could try this command: echo $SHELL – slm Oct 22 '13 at 20:07
    
@slm yes, it's bash. /bin/bash. – Bleeding Fingers Oct 22 '13 at 20:08
    
$! is (at least) POSIX so not specific to bash. I'd assume it's universal to sh. – goldilocks Oct 23 '13 at 15:52
up vote 13 down vote accepted

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

share|improve this answer

$! 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.

share|improve this answer

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