64

If I'm assigning a variable with

temp=$!

what would it be its value?

2 Answers 2

89

$! contains the process ID of the most recently executed background pipeline. From man bash:

Special Parameters

The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed.

...

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

For example:

$ sleep 60 &
[1] 6238
$ echo "$!"
6238
1
  • I get the feeling that you and slm are saying about the same thing :)
    – msw
    Jul 31, 2013 at 10:56
30

From Bash's man page:

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

So $! would contain the process ID (PID) of the last job that was backgrounded.

Example

$ sleep 100 &
[1] 18813

$ echo "$!"
18813

References

4
  • 1
    I get the feeling that you and Chris Down are saying about the same thing :)
    – user
    Jul 31, 2013 at 9:25
  • 2
    It would seem there's an echo on this Question 8-).
    – slm
    Jul 31, 2013 at 11:30
  • Same votes counts, too!
    – user
    Jul 31, 2013 at 12:03
  • @MichaelKjörling - hey great minds think alike 8-).
    – slm
    Jul 31, 2013 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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